void Ouput(const char b[M][M])
{
int i, j;
printf("迷宫的形状为:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
{
printf("%c",b[i][j]);
}
printf("\n");
}
}
int FindWay(char maze[M][M])
{
ElemType e;
int constep = 1;
int x = 1, y = 1;
SqStack S;
InitStack(&S);
do
{
if (maze[x][y] == ' ')//当第3次时 maze[x][y]!=' ' 照样通不过
{
maze[x][y] = '1';
e.x = x;
e.y = y;
e.dir = 1;
Push(&S,e);
if (x == M-2 && y == M-2)
{
printf("存在出口\n");
return 1;
}
NextStep(&x,&y,1);
constep++;
}
else
{
Pop(&S,&e);
while (e.dir == 4 && !StackEmpty(&S))
{
maze[e.x][e.y] = '0';
Pop(&S,&e);
}
{
if (e.dir < 4)
{
e.dir++;
Push(&S,e);
x = e.x;
y = e.y;
NextStep(&x, &y, e.dir);
}
else
{
printf("没有出口\n");
return 0;
}
}
}
}while(S.top!=S.base);
return 0;
}
int NextStep(int *x, int *y, int dir)
{
switch(dir)
{
case 1:
(*y)++;
break;
case 2:
(*x)++;
break;
case 3:
(*y)--;
break;
case 4:
(*x)--;
break;
default:
break;
}
return 0;
}
int main(void)
{
char a[M][M];
Input(a);
Ouput(a);
FindWay(a);
Ouput(a);
system("pause");
return 0;
}