迷宫有一个入口,一个出口。一个人从入口走进迷宫,目标是找到出口。阴影部分和迷宫的外框为墙,每一步走一格,每格有四个可走的方向,探索顺序为地图方向:南(下)、东(右)、北(上)、西(左)。
#include <stdio.h>
#include <stack>
#include <stdlib.h>
#include <string.h>
#define N 50
using namespace std;
char map[N][N] = {2};
int main()
{
stack<int> x, y;
int m, n;
int i, j;
scanf("%d %d", &m, &n);
for (i = 0; i <= m + 1; i++)
{
for (j = 0; j <= n + 1; j++)
{
if (i * j != 0 && i <= m && j <= n)
{
scanf("%d", &map[i][j]);
}
else
{
map[i][j] = 2;
}
}
}
if (map[1][1] == 1)
{
printf("There is no solution!\n");
return 0;
}
x.push(m);
y.push(n);
while (true)
{
if (x.size() == 0)
{
printf("There is no solution!\n");
return 0;
}
i = x.top();
j = y.top();
// printf("%d %d %d\n", x.size(), i, j);
if (i == 1 && j == 1)
{
break;
}
if (map[i - 1][j] == 0)
{
x.push(i - 1);
y.push(j);
map[i - 1][j] = 2;
}
else if (map[i][j - 1] == 0)
{
x.push(i);
y.push(j - 1);
map[i][j - 1] = 2;
}
else if (map[i + 1][j] == 0)
{
x.push(i + 1);
y.push(j);
map[i + 1][j] = 2;
}
else if (map[i][j + 1] == 0)
{
x.push(i);
y.push(j + 1);
map[i][j + 1] = 2;
}
else
{
x.pop();
y.pop();
}
}
while (!x.empty())
{
printf("<%d,%d> ", x.top(), y.top());
x.pop();
y.pop();
}
printf("\n");
return 0;
}