一天蒜头君掉进了一个迷宫里面,蒜头君想逃出去,可怜的蒜头君连迷宫是否有能逃出去的路都不知道。
看在蒜头君这么可怜的份上,就请聪明的你告诉蒜头君是否有可以逃出去的路。
输入格式
第一行输入两个整数 nn 和 mm,表示这是一个 n \times mn×m 的迷宫。
接下来的输入一个 nn 行 mm 列的迷宫。其中 'S'
表示蒜头君的位置,'*'
表示墙,蒜头君无法通过,'.'
表示路,蒜头君可以通过'.'
移动,'T'
表示迷宫的出口(蒜头君每次只能移动到四个与他相邻的位置——上,下,左,右)。
输出格式
输出一个字符串,如果蒜头君可以逃出迷宫输出"yes"
,否则输出"no"
。
数据范围
1 \le n, m \le 101≤n,m≤10。
Sample Input
3 4
S**.
..*.
***T
Sample Output
no
Sample Input 2
3 4
S**.
....
***T
Sample Output 2
yes
代码如下:
#include<bits/stdc++.h>
using namespace std;
char mg[11][11];
int n, m;
bool pd = false;
bool vis[11][11];
int dx[] = { -1,0,1,0 };
int dy[] = { 0,-1,0,1 };
void dfs(int x, int y)
{
vis[x][y] = true;
if (mg[x][y] == 'T')
{
pd = true;
return ;
}
for (int i = 0; i < 4; i++)
{
int xt = x + dx[i];
int yt = y + dy[i];
if (xt < 0 || xt >= n || yt < 0 || yt >= m)
continue;
if (vis[xt][yt])
continue;
if (mg[xt][yt] == '*')
continue;
dfs(xt, yt);
}
}
int main()
{
cin >> n >> m;
int i, j,h,l;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
{
cin >> mg[i][j];
if (mg[i][j] == 'S')
{
h = i;
l = j;
}
}
dfs(h, l);
if (pd)
cout << "yes" << endl;
else
cout << "no" << endl;
return 0;
}