1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include<stdio.h> #include "sqstack.h"
#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define m 8 #define n 8
typedef int Status; typedef int MazeType;
PosType dire[4] = { {0,1},{1,0},{0,-1},{-1,0} }; PosType start,end;
MazeType maze[m+2][n+2] = { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,1,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,1,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}, };
Status printPath(SqStack *p, int* PathCount); Status MazePath(int maze[][n+2], int* PathCount, int x, int y, SqStack *p); Status MarkPrint(SElemType e);
Status main() { SqStack mazeStack; InitStack(&mazeStack);
int PathCount = 0; start.x = 1; start.y = 1; end.x = m; end.y = n;
MazePath(maze, &PathCount,start.x,start.y,&mazeStack);
DestroyStack(&mazeStack); return OK; }
Status printPath(SqStack *p, int* PathCount) { *PathCount += 1; printf("Path %d :\n", *PathCount); SElemType e; for (SElemType* i = p->base; i < p->top; i++) { e = *i; printf("(%d,%d)->", e.seat.x, e.seat.y); } printf("Exit Found!\n\n"); return OK; }
Status MarkPrint(SElemType e) { maze[e.seat.x][e.seat.y] = -1; return OK; }
Status MazePath(int maze[][n+2], int* PathCount, int x, int y, SqStack *p) {
if (x == end.x && y == end.y) { maze[x][y] = -1;
SElemType e; e.seat.x = x; e.seat.y = y; Push(p, e);
printPath(p, PathCount);
Pop(p, &e); maze[x][y] = 0;
return TRUE; } else { if (maze[x][y] == 0) { int di = 0;
while (di < 4) {
SElemType e; e.seat.x = x; e.seat.y = y;
MarkPrint(e); Push(p, e);
int i, j; i = x + dire[di].x; j = y + dire[di].y;
MazePath(maze, PathCount, i, j, p);
Pop(p, &e); maze[x][y] = 0;
di++; } } else return OK; } return OK; }
|