a perfect maze, there is one and only one path from any point in the
maze to any other point. That is, there are no inaccessible sections,
no circular paths, and no open regions. A perfect maze can be generated
easily with a computer using a depth first search algorithm.
A two dimensional maze can be represented as a rectangular array of
square cells. Each cell has four walls. The state of each wall (north,
south, east, and west) of each cell is maintained in a data structure
consisting of an array of records. Each record stores a bit value that
represents the state of each wall in a cell. To create a path between
adjacent cells, the exit wall from the current cell and the entry wall
to the next cell are removed. For example, if the next cell is to the
right (east) of the current cell, remove the right (east) wall of the
current cell and the left (west) wall of the next cell.
01 | create a CellStack (LIFO) to hold a list of cell locations |
02 | set TotalCells = number of cells in grid |
03 | choose a cell at random and call it CurrentCell |
06 | while VisitedCells < TotalCells |
08 | find all neighbors of CurrentCell with all walls intact |
11 | knock down the wall between it and CurrentCell |
12 | push CurrentCell location on the CellStack |
13 | make the new cell CurrentCell |
14 | add 1 to VisitedCells else |
15 | pop the most recent cell entry off the CellStack |
16 | make it CurrentCell endIf |
Here it this..the source code :
011 | void init_maze( int maze[MAX][MAX]); |
012 | void maze_generator( int indeks, int maze[MAX][MAX], int backtrack_x[CELL], int bactrack_y[CELL], int x, int y, int n, int visited); |
013 | void print_maze( int maze[MAX][MAX], int maze_size); |
014 | int is_closed( int maze[MAX][MAX], int x, int y); |
018 | srand((unsigned)time(NULL)); |
022 | printf( "MAZE CREATOR\n\n" ); |
023 | printf( "input (0 ~ 30): " ); |
027 | int backtrack_x[CELL]; |
028 | int backtrack_y[CELL]; |
032 | backtrack_x[indeks] = 1 ; |
033 | backtrack_y[indeks] = 1 ; |
035 | maze_generator(indeks, maze, backtrack_x, backtrack_y, 1 , 1 , size, 1 ); |
036 | print_maze(maze, size); |
042 | void init_maze( int maze[MAX][MAX]) |
044 | for ( int a = 0 ; a < MAX; a++) |
046 | for ( int b = 0 ; b < MAX; b++) |
048 | if (a % 2 == 0 || b % 2 == 0 ) |
056 | void maze_generator( int indeks, int maze[MAX][MAX], int backtrack_x[CELL], int backtrack_y[CELL], int x, int y, int n, int visited) |
060 | int neighbour_valid = - 1 ; |
068 | if (x - 2 > 0 && is_closed(maze, x - 2 , y)) |
071 | neighbour_x[neighbour_valid]=x - 2 ;; |
072 | neighbour_y[neighbour_valid]=y; |
073 | step[neighbour_valid]= 1 ; |
076 | if (y - 2 > 0 && is_closed(maze, x, y - 2 )) |
079 | neighbour_x[neighbour_valid]=x; |
080 | neighbour_y[neighbour_valid]=y - 2 ; |
081 | step[neighbour_valid]= 2 ; |
084 | if (y + 2 < n * 2 + 1 && is_closed(maze, x, y + 2 )) |
087 | neighbour_x[neighbour_valid]=x; |
088 | neighbour_y[neighbour_valid]=y + 2 ; |
089 | step[neighbour_valid]= 3 ; |
093 | if (x + 2 < n * 2 + 1 && is_closed(maze, x + 2 , y)) |
096 | neighbour_x[neighbour_valid]=x+ 2 ; |
097 | neighbour_y[neighbour_valid]=y; |
098 | step[neighbour_valid]= 4 ; |
101 | if (neighbour_valid == - 1 ) |
104 | x_next = backtrack_x[indeks]; |
105 | y_next = backtrack_y[indeks]; |
109 | if (neighbour_valid!=- 1 ) |
111 | int randomization = neighbour_valid + 1 ; |
112 | int random = rand()%randomization; |
113 | x_next = neighbour_x[random]; |
114 | y_next = neighbour_y[random]; |
116 | backtrack_x[indeks] = x_next; |
117 | backtrack_y[indeks] = y_next; |
119 | int rstep = step[random]; |
122 | maze[x_next+ 1 ][y_next] = PATH; |
124 | maze[x_next][y_next + 1 ] = PATH; |
126 | maze[x_next][y_next - 1 ] = PATH; |
128 | maze[x_next - 1 ][y_next] = PATH; |
132 | maze_generator(indeks, maze, backtrack_x, backtrack_y, x_next, y_next, n, visited); |
136 | void print_maze( int maze[MAX][MAX], int maze_size) |
138 | for ( int a = 0 ; a < maze_size * 2 + 1 ; a++) |
140 | for ( int b = 0 ; b < maze_size * 2 + 1 ; b++) |
142 | if (maze[a][b] == WALL) |
151 | int is_closed( int maze[MAX][MAX], int x, int y) |
153 | if (maze[x - 1 ][y] == WALL |
154 | && maze[x][y - 1 ] == WALL |
155 | && maze[x][y + 1 ] == WALL |
156 | && maze[x + 1 ][y] == WALL |