======================================== Maze FAQ ======================================== Sorry this is NOT an organized FAQ it's still useful though ------------------------------------------------------------ Oh, you're really going to love this one. It's an obfuscated C code mazegenerator. Fun fun fun. Well, if you can figure it out, there's youralgorithm. Fun fun fun. char*M,A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf(M="%d",&C); -- E; J[ E] =T [E ]= E) printf("._"); for(;(A-=Z=!Z) || (printf("\n|" ) , A = 39 ,C -- ) ; Z || printf (M ))M[Z]=Z[A-(E =A[J-Z])&&!C & A == T[ A] |6<<11 rcarter@wpi.WPI.EDU (Randolph Carter (nee. Joseph H. Allen)) writes: >>char*M,A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf(M="%d",&C); >>-- E; J[ E] =T >>[E ]= E) printf("._"); for(;(A-=Z=!Z) || (printf("\n|" >>) , A = 39 ,C -- >>) ; Z || printf (M ))M[Z]=Z[A-(E =A[J-Z])&&!C >>& A == T[ A] >>|6<<11>Pretty cute, no? >No style at all.... :-) ======================================== >-- >/* rcarter@wpi.wpi.edu */ /* Amazing */ /* Joseph H. Allen */ >int a[1817];main(z,p,q,r){for(p=80;q+p-80;p-=2*a[p])for(z=9;z--;)q=3&(r=time(0) >+r*57)/7,q=q?q-1?q-2?1-p%79?-1:0:p%79-77?1:0:p<1659?79:0:p>158?-79:0,q?!a[p+q* 2 >]?a[p+=a[p+=q]=q]=q:0:0;for(;q++-1817;)printf(q%79?"%c":"%c\n"," #"[!a[q-1]]);} Well, it doesn't produce a maze, but try this one... int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c -=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);} (I disclaim any credit for this!) -- John Brownie School of Mathematics and Statistics University of Sydney Internet: jhb@maths.su.oz.au ======================================== Excerpts from programming: 6-Mar-92 Re: Algorithm to create a m.. Mark Howell@movies.enet. (5723) Here's the single level maze algorithm, solver and printer. /* * MazeGen.c -- Mark Howell -- 8 May 1991 * * Usage: MazeGen [width [height [seed]]] */ #include #include #include #define WIDTH 39 #define HEIGHT 11 #define UP 0 #define RIGHT 1 #define DOWN 2 #define LEFT 3 #ifdef TRUE #undef TRUE #endif /* TRUE */ #define TRUE 1 #define cell_empty(a) (!(a)->up && !(a)->right && !(a)->down && !(a)->left) typedef struct { unsigned int up : 1; unsigned int right : 1; unsigned int down : 1; unsigned int left : 1; unsigned int path : 1; unsigned int visited : 1; } cell_t; typedef cell_t *maze_t; void CreateMaze (maze_t maze, int width, int height); void SolveMaze (maze_t maze, int width, int height); void PrintMaze (maze_t maze, int width, int height); int main (int argc, char *argv []) { int width = WIDTH; int height = HEIGHT; maze_t maze; if (argc >= 2) width = atoi (argv [1]); if (argc >= 3) height = atoi (argv [2]); if (argc >= 4) srand (atoi (argv [3])); else srand ((int) time ((time_t *) NULL)); if (width <= 0 || height <= 0) { (void) fprintf (stderr, "Illegal width or height value!\n"); exit (EXIT_FAILURE); } maze = (maze_t) calloc (width * height, sizeof (cell_t)); if (maze == NULL) { (void) fprintf (stderr, "Cannot allocate memory!\n"); exit (EXIT_FAILURE); } CreateMaze (maze, width, height); PrintMaze (maze, width, height); (void) putchar ('\n'); SolveMaze (maze, width, height); PrintMaze (maze, width, height); free (maze); exit (EXIT_SUCCESS); return (0); }/* main */ void CreateMaze (maze_t maze, int width, int height) { maze_t mp, maze_top; char paths [4]; int visits, directions; visits = width * height - 1; mp = maze; maze_top = mp + (width * height) - 1; while (visits) { directions = 0; if ((mp - width) >= maze && cell_empty (mp - width)) paths [directions++] = UP; if (mp < maze_top && ((mp - maze + 1) % width) && cell_empty (mp + 1)) paths [directions++] = RIGHT; if ((mp + width) <= maze_top && cell_empty (mp + width)) paths [directions++] = DOWN; if (mp > maze && ((mp - maze) % width) && cell_empty (mp - 1)) paths [directions++] = LEFT; if (directions) { visits--; directions = ((unsigned) rand () % directions); switch (paths [directions]) { case UP: mp->up = TRUE; (mp -= width)->down = TRUE; break; case RIGHT: mp->right = TRUE; (++mp)->left = TRUE; break; case DOWN: mp->down = TRUE; (mp += width)->up = TRUE; break; case LEFT: mp->left = TRUE; (--mp)->right = TRUE; break; default: break; } } else { do { if (++mp > maze_top) mp = maze; } while (cell_empty (mp)); } } }/* CreateMaze */ void SolveMaze (maze_t maze, int width, int height) { maze_t *stack, mp = maze; int sp = 0; stack = (maze_t *) calloc (width * height, sizeof (maze_t)); if (stack == NULL) { (void) fprintf (stderr, "Cannot allocate memory!\n"); exit (EXIT_FAILURE); } (stack [sp++] = mp)->visited = TRUE; while (mp != (maze + (width * height) - 1)) { if (mp->up && !(mp - width)->visited) stack [sp++] = mp - width; if (mp->right && !(mp + 1)->visited) stack [sp++] = mp + 1; if (mp->down && !(mp + width)->visited) stack [sp++] = mp + width; if (mp->left && !(mp - 1)->visited) stack [sp++] = mp - 1; if (stack [sp - 1] == mp) --sp; (mp = stack [sp - 1])->visited = TRUE; } while (sp--) if (stack [sp]->visited) stack [sp]->path = TRUE; free (stack); }/* SolveMaze */ void PrintMaze (maze_t maze, int width, int height) { int w, h; char *line, *lp; line = (char *) calloc ((width + 1) * 2, sizeof (char)); if (line == NULL) { (void) fprintf (stderr, "Cannot allocate memory!\n"); exit (EXIT_FAILURE); } maze->up = TRUE; (maze + (width * height) - 1)->down = TRUE; for (lp = line, w = 0; w < width; w++) { *lp++ = '+'; if ((maze + w)->up) *lp++ = ((maze + w)->path) ? '.' : ' '; else *lp++ = '-'; } *lp++ = '+'; (void) puts (line); for (h = 0; h < height; h++) { for (lp = line, w = 0; w < width; w++) { if ((maze + w)->left) *lp++ = ((maze + w)->path && (maze + w - 1)->path) ? '.' : ' '; else *lp++ = '|'; *lp++ = ((maze + w)->path) ? '.' : ' '; } *lp++ = '|'; (void) puts (line); for (lp = line, w = 0; w < width; w++) { *lp++ = '+'; if ((maze + w)->down) *lp++ = ((maze + w)->path && (h == height - 1 || (maze + w + width)->path)) ? '.' : ' '; else *lp++ = '-'; } *lp++ = '+'; (void) puts (line); maze += width; } free (line); }/* PrintMaze */ ======================================== Excerpts from programming: 6-Mar-92 Re: Algorithm to create a m.. "Jon C. R. Bennett"@andr (4255) gillies@m.cs.uiuc.edu (Don Gillies) writes: > grid. Mark each square in the grid with a unique number. Make a list what you want to do is make each grid in the maze into a set. > > rooms = n*n /* each spot in the grid is a unique room */ > > repeat > pick a random wall without replacement. > if the numbers X and Y in the grid on both sides of the wall > are different -- > delete the wall and use a recursive depth > first search or brute-force loop to replace > all Y in the grid with X's. what you do here is instead pick a wall if the rooms on either side of the wall belong to differnent sets delete the wall union the two sets together. the rest is the same the brute force solution runs in O(n^2) this runs in O(n) (where n is the number of grids) so if you had a 100 x 100 maze, this method takes 10,000 time steps, the brute force could take as many as 100,000,000 steps. jon p.s. below you will find some code to generate a maze this way --------------------------------------------------- /* maze.c a maze generator Jon Bennett jcrb@cs.cmu.edu */ /* the maze is generated by making a list of all the internal hedges and randomizing it, then going lineraly through the list, we take a hedge and se if the maze squares adjacent to it are already connected (with find) is not the we connect them (with link), this prevents us from creating a maze with a cycle because we will not link two maze squares that are already connect by some path */ #include #define DOWN 1 #define RIGHT 2 struct maze_loc{ int rank; int x,y; struct maze_loc *ptr; }; struct hedge_loc{ int x,y,pos,on; }; struct maze_loc *maze; struct hedge_loc *hedge; struct hedge_loc **hedge_list; void link(a,b) struct maze_loc *a,*b; { if(a->rank == b->rank){ a->ptr=b; b->rank++; return; } if(a->rank > b->rank){ b->ptr=a; return; } a->ptr=b; } struct maze_loc *find(a) struct maze_loc *a; { if(a != a->ptr){ a->ptr = find(a->ptr); } return a->ptr; } main(argc,argv) int argc; char **argv; { int x,y,i,j,k,l,tmp; struct maze_loc *a,*b; struct hedge_loc *htmp; if(argc!=3) exit(1); srandom(time(0)); x=atoi(argv[1]); y=atoi(argv[2]); /*malloc the maze and hedges */ maze=(struct maze_loc *)malloc(sizeof(struct maze_loc)*x*y); hedge=(struct hedge_loc *)malloc(sizeof(struct hedge_loc)*((x*(y-1))+((x-1)*y))); hedge_list=(struct hedge_loc **)malloc(sizeof(struct hedge_loc *)*((x*(y-1))+((x-1)*y))); /*init maze*/ for(j=0;j0;i--){ htmp=hedge_list[i]; j=random()%i; hedge_list[i]=hedge_list[j]; hedge_list[j]=htmp; } fflush(stdout); l=k; /*create maze*/ for(i=0;ix; k=hedge_list[i]->y; a=find(&maze[x*k+j]); if(hedge_list[i]->pos==DOWN){ b=find(&maze[x*(k+1)+j]); } else { b=find(&maze[x*k+j+1]); } if(a!=b){ link(a,b); hedge_list[i]->on=0; } } printf("+"); for(i=0;i 1 do the following: Remove a wall from the set of walls at random. If the two cells that this wall separates are already connected (test by doing a FIND on each), then do nothing; otherwise, connect the two cells (by UNIONing them and decrementing COUNT) and knock out the wall. Note that none of these algorithms make any assumptions about the topology of the maze. They will work with 2-d or 3-d grids, toroids, hexagons, whatever. However, in the more highly connected topologies (such as 3-d grids), the deficiencies of the first algorithm will become even more apparent (it will tend to produce long, winding paths with very little branching). Have fun with these! Chris ================================ /* * maz.c - generate a maze * * algorithm posted to rec.games.programmer by jallen@ic.sunysb.edu * program cleaned and reorganized by mzraly@ldbvax.dnet.lotus.com * * don't make people pay for this, or I'll jump up and down and * yell and scream and embarass you in front of your friends... * * compile: cc -o maz -DDEBUG maz.c * */ #include static int multiple = 57; /* experiment with this? */ static int offset = 1; /* experiment with this? */ #ifdef __STDC__ int maze(char maz[], int y, int x, char vc, char hc, char fc); void mazegen(int pos, char maz[], int y, int x, int rnd); #else int maze(); void mazegen(); #endif /* * maze() : generate a random maze of size (y by x) in maz, using vc as the * vertical character, hc as the horizontal character, and fc as the floor * character * * maz is an array that should already have its memory allocated - you could * malloc a char string if you like. */ #ifdef __STDC__ int maze(char maz[], int y, int x, char vc, char hc, char fc) #else int maze(maz, y, x, vc, hc, fc) char maz[], vc, hc, fc; int y, x; #endif { int i, yy, xx; int max = (y * x); int rnd = time(0L); /* For now, return error on even parameters */ /* Alternative is to decrement evens by one */ /* But really that should be handled by caller */ if (!(y & 1) | !(x & 1)) return (1); /* I never assume... */ for (i = 0; i < max; ++i) maz[i] = 0; (void) mazegen((x + 1), maz, y, x, rnd); /* Now replace the 1's and 0's with appropriate chars */ for (yy = 0; yy < y; ++yy) { for (xx = 0; xx < x; ++xx) { i = (yy * x) + xx; if (yy == 0 || yy == (y - 1)) maz[i] = hc; else if (xx == 0 || xx == (x - 1)) maz[i] = vc; else if (maz[i] == 1) maz[i] = fc; else if (maz[i - x] != fc && maz[i - 1] == fc && (maz[i + x] == 0 || (i % x) == (y - 2))) maz[i] = vc; else maz[i] = hc; /* for now... */ } } return (0); } /* * mazegen : do the recursive maze generation * */ #ifdef __STDC__ void mazegen(int pos, char maz[], int y, int x, int rnd) #else void mazegen(pos, maz, y, x, rnd) int pos, y, x, rnd; char maz[]; #endif { int d, i, j; maz[pos] = 1; while (d = (pos <= x * 2 ? 0 : (maz[pos - x - x] ? 0 : 1)) | (pos >= x * (y - 2) ? 0 : (maz[pos + x + x] ? 0 : 2)) | (pos % x == x - 2 ? 0 : (maz[pos + 2] ? 0 : 4)) | (pos % x == 1 ? 0 : (maz[pos - 2] ? 0 : 8))) { do { rnd = (rnd * multiple + offset); i = 3 & (rnd / d); } while (!(d & (1 << i))); switch (i) { case 0: j = -x; break; case 1: j = x; break; case 2: j = 1; break; case 3: j = -1; break; default: break; } maz[pos + j] = 1; mazegen(pos + 2 * j, maz, y, x, rnd); } return; } #ifdef DEBUG #define MAXY 24 #define MAXX 80 #ifdef __STDC__ main(int argc, char *argv[]) #else main(argc, argv) int argc; char *argv[]; #endif { extern int optind; extern char *optarg; int x = 79; int y = 23; char hor = '-'; char ver = '|'; char flo = ' '; char maz[MAXY * MAXX]; int i; while ((i = getopt(argc, argv, "h:v:f:y:x:m:o:")) != EOF) switch (i) { case 'h': hor = *optarg; break; case 'v': ver = *optarg; break; case 'f': flo = *optarg; break; case 'y': y = atoi(optarg); break; case 'x': x = atoi(optarg); break; case 'm': multiple = atoi(optarg); break; case 'o': offset = atoi(optarg); break; case '?': default: (void) fprintf(stderr, "usage: maz [xyhvfmo]\n"); break; } if (maze(maz, y, x, ver, hor, flo) == 0) { for (i = 0; i < (x * y); ++i) { (void) putchar(maz[i]); if (((i + 1) % x) == 0) (void) putchar('\n'); } } else { (void) fprintf(stderr, "Couldn't make the maze\n"); } exit(0); } #endif DEBUG ���� hor = *optarg; break; case 'v': ver = *optarg; break; case 'f': flo = *optarg; break; case 'y': y = atoi(optarg); break; case 'x': x = atoi(optarg); break; case 'm': multiple = atoi(optarg); break; case 'o': offset = atoi(optarg); break; case '?': default: (void) fprintf(stderr, "usage: maz [xyhvfmo]\n"); break; } if (maze(maz, y, x, ver, hor, flo) == 0) { for (i = 0; i < (x * y); ++i) { (void) putchar(maz[i]); if (((i + 1) % x) == 0) (void) putchar('\n'); } } else { (void) fprintf(stderr, "Couldn't make the maze\n"); } exit(0); } #endif DEBUG ������