题目描述
输入一个n*m (n,m <= 100)
的迷宫,检查是否能从起点出发,并走到终点,如果能的话输出"yes",否则输出"no"。其中"S"表示起点,"E"表示终点,"."表示可通行的路径,"#"表示墙壁。
输入描述
5 5
S . . # .
. # . # .
. # . . .
# # # . .
. . . . E
输出描述
yes
1, 1
1, 2
1, 3
2, 3
3, 3
3, 4
4, 4
5, 4
5, 5
输入样例
5 5
S..#.
.#.#.
.#...
###..
....E
输出样例
yes
1, 1
1, 2
1, 3
2, 3
3, 3
3, 4
3, 5
4, 5
4, 4
5, 4
5, 5
#include <cstdio>
#include <iostream>
using namespace std;
int n, m, stx, sty, edx, edy, flg, map[110][110];
int movx[4] = {0, 0, -1, 1}, movy[4] = {-1, 1, 0, 0};
int psw[10010][2], cnt; //new
char s[110];
void DFS (int x, int y) {
cnt++; //new
psw[cnt][0] = x; //new
psw[cnt][1] = y; //new
map[x][y] = 0;
if (x == edx && y == edy) {
flg = 1;
return;
}
for (int i = 0; i < 4; i++)
if (map[x + movx[i]][y + movy[i]] == 1) {
DFS(x + movx[i], y + movy[i]);
if (flg == 1) //new
return; //new
}
cnt--; //new
return;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
for (int j = 1; j <= m; j++) {
switch (s[j]) {
case '.': map[i][j] = 1;break;
case '#': map[i][j] = 0;break;
case 'S': map[i][j] = 1; stx = i; sty = j;break;
case 'E': map[i][j] = 1; edx = i; edy = j;break;
}
}
}
DFS(stx, sty);
if (flg == 1)
cout << "yes" << endl;
else
cout << "no" << endl;
//new 逐行输出经过点的坐标(包含起点和终点)
for (int i = 1; i <= cnt; i++)
cout << psw[i][0] << ", " << psw[i][1] << endl;
return 0;
}