using stacks to solve basic maze problems C + +
1, the problem description: Set the maze as a m*n two-dimensional array, starting coordinates (1,1), midpoint coordinates (M,N), 0 for the path, 1 for dead end, in order to prevent the array out of bounds to set the perimeter 1, that is, the array into (m+2) * (n+2) array, the maze is as follows ....
Maze
| 1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
| 1 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
| 1 |
1 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
| 1 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
| 1 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
| 1 |
1 |
0 |
0 |
1 |
1 |
0 |
0 |
0 |
1 |
| 1 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
| 1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2, solve the idea:
2.1, with the array of the definition of the maze
#define M 6
#define N 8
int maze[m+2][n+2]
2.2. Temptation Direction
Under normal circumstances, each point has eight directions to be tempted, the point coordinates (X,Y) can be added to the coordinates of the points and minus 1 for the position of the transformation, that is, add a move array, defined as follows:
typedef struct{
int x,y;
} item;
Item move[8];
Assign a value to move
void Definemove (item move[8]) {
move[0].x = 0,move[0].y =1;
move[1].x = 1,move[1].y =1;
xmove[2].x = 1,xmove[2].y =0;
move[3].x = 1,move[3].y =-1;
move[4].x = 0,move[4].y =-1;
move[5].x = -1,move[5].y =-1;
move[6].x = 1,move[6].y =0;
move[7].x = -1,move[7].y =1;
}
Operation when moving: x = x+move[i].x; y = y+move[i].y;
2.3, the design of the stack:
|
|
|
|
| 3,6,0 (top) |
| 3,5,0 |
| 3,4,0 |
| 3,3,0 |
| 2,2,1 |
| 1,1,1 |
First, the starting point into the stack, and from the right point of the search road, if the 0, that is, to carry out the stack, if it does not pass the point of backtracking, looking for other directions.
The definition of the stack:
The definition of the point is
typedef struct{
int x,y,d;
} Point
Stack definition
typedef struct{Point
data[maxsize];
int top;
} Mazestack;
2.4, to prevent the repetition of a point, lest the death cycle, will go through the point of initialization to-1, that is maze[x][y] =-1.
2.5, the maze algorithm to solve the idea:
(1), the stack is initialized
(2), the entry point coordinates and the direction to reach the point (set to-1) into the stack
(3), while (stack is not empty)
{Stack top element => (x,y,d)
Out Stack
Find the next direction to test d++
While (there is also the remaining temptation direction)
{if (d direction to go)
The
(x,y,d) into the stack
Find the coordinates of the new point (I,J)
Toggles the new Point (I,J) to the current point (X,y)
if (x,y) = = (M,n) end
else reset d++;
}
}
3, all the code:
* * Use sequential stack to solve maze problem * * #include <stdio.h> #include <malloc.h> #define MAXSIZE #define M 6 #define N 8//to the elements in the stack Row definition, D is the direction typedef struct{INT x,y,d;
Point
Define the structure of the stack typedef struct{Point data[maxsize];
int top;
}mazestack; Define the moving array to facilitate the movement of the point of the typedef struct{int x,y;}
Item Set stack to empty stack void SetNull (Mazestack *s) {s->top =-1;}//Determine if stack is empty bool IsEmpty (Mazestack *s) {if (s->top>=0) return
False
else return true; }//Stack operation Mazestack * PUSH (mazestack *s,point x) {if (s->top>maxsize-1) {printf ("Overflow on stack").
\ n ");
return s;
}else{s->top++;
S->data[s->top] = x;
return s; }//Fallback operation point * POP (Mazestack *s) {if (IsEmpty (s)) {printf ("stack is empty.
\ n ");
return NULL;
}else{s->top--;
Return & (S->data[s->top+1]); }///fetch stack top element point * GETTOP (Mazestack *s) {if (IsEmpty (s)) {printf ("stack is empty.
\ n ");
return NULL;
}else{return & (S->data[s->top]); }///define the location of the move void Definemove (item xmove[8]) {xmove[0].x = 0,xmove[0].Y = 1;
xmove[1].x = 1,XMOVE[1].Y = 1;
xmove[2].x = 1,xmove[2].y = 0;
xmove[3].x = 1,xmove[3].y =-1;
xmove[4].x = 0,xmove[4].y =-1;
xmove[5].x = -1,xmove[5].y =-1;
xmove[6].x = 1,xmove[6].y = 0;
xmove[7].x = -1,XMOVE[7].Y = 1;
The test int main () {//The maze is defined int maze[m+2][n+2],x,y,i,j,d;
Define the location of the move item xmove[8];
Define the starting point of the stack points start,*p;
The stack is defined mazestack *s;
s = (mazestack*) malloc (sizeof (mazestack));
SetNull (s);
Define the position of the move definemove (Xmove);
Enter printf for the maze ("Enter the maze: \ n");
for (i = 0;i<m+2;i++) for (j = 0;j<n+2;j++) scanf ("%d", &maze[i][j]);
Start.x = 1;
Start.y = 1;
START.D =-1;
p = (point*) malloc (sizeof (point));
The starting point is pressed into the stack s = push (S,start);
while (!isempty (s)) {p = pop (s);
x = p->x;
y = p->y;
D = p->d+1;
while (d<8) {i = xmove[d].x+x;
j = xmove[d].y+y;
if (maze[i][j]==0) {p->d = D;
s = push (S,*P);
x = i; y = J
Maze[x][y] =-1;
Point NW;
nw.x = x;
Nw.y = y;
NW.D =-1;
s = push (S,NW); if (x==m&&y==n) {printf ("Find the exit.")
\ n ");
while (!isempty (s)) {p = pop (s);
printf ("%d%d%d\n", p->x,p->y,p->d);
return 1;
}else{break;
}}else{d++;
}} return 0;
}