0%

Hero In Maze

题面

输入

1
2
3
4
5
6
4 4 10
....
....
....
S**P
0 0 0

输出

1
YES

代码

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
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<unordered_map>
#include<queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 20;
int n, m, k, ax, ay, bx, by;
char map[N][N];
queue<PII> q;
int dist[N][N];
int dx[4] = { 1,-1,0,0 }, dy[4] = { 0,0,1,-1 };

int bfs()
{
q.push({ ax,ay });
dist[ax][ay] = 0;

while (!q.empty())
{
PII t = q.front();
q.pop();

int x = t.first, y = t.second;

if (x == bx && y == by) return true;

for (int i = 0; i <= 3; i++)
{
int xx = x + dx[i], yy = y + dy[i];
if (xx >= 0 && xx < m && yy >= 0 && yy < n && dist[xx][yy]==-1 && map[xx][yy]!='*')
{
dist[xx][yy] = dist[x][y] + 1;
if(dist[xx][yy]<=k) q.push({ xx,yy });
}
}
}

return false;
}
int main()
{
while (cin >> n >> m >> k && n && m && k )
{
memset(map, ' ', sizeof map);
memset(dist, -1, sizeof dist);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
cin >> map[i][j];
if (map[i][j] == 'S')ax = i, ay = j;
if (map[i][j] == 'P')bx = i, by = j;
}
if (bfs())cout << "YES"<<endl;
else cout << "NO" << endl;
}
return 0;
}
-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道