-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path308-4.cpp
More file actions
73 lines (73 loc) · 1.63 KB
/
308-4.cpp
File metadata and controls
73 lines (73 loc) · 1.63 KB
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
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
string s[501];
bool visited[501][501];
int n, m;
string mod = "esnuk";//注意细节问题,求模的结果处理
typedef pair<int, int> p;
int dis[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
struct node
{
p pos={0,0};
int step=0;
int manhaton=0;
bool operator<(const node &x) const
{
return x.manhaton < manhaton;
}
node()
{};
node(p pos,int step)
{
this->pos=pos;
this->step=step;
this->manhaton=abs(pos.first-n)+abs(pos.second-m);
};
};
void bfs()
{
priority_queue<node> q;
q.push(node({1, 1}, 1));
if (s[1][0] != 's')
{
std::cout << "No" << endl;
return;
}
while (!q.empty())
{
node top = q.top();
q.pop();
if (top.pos == p{n, m})
{
std::cout << "Yes" << endl;
return;
}
if (visited[top.pos.first][top.pos.second])
continue;
visited[top.pos.first][top.pos.second] = 1;
for (int i = 0; i < 4; i++)
{
node next({top.pos.first + dis[i][0], top.pos.second + dis[i][1]}, top.step + 1);
if (next.pos.first >= 1 && next.pos.first <= n && next.pos.second >= 1 && next.pos.second <= m)
{
if (mod[next.step % 5] == s[next.pos.first][next.pos.second - 1])
{
q.push(next);
}
}
}
}
std::cout << "No" << endl;
return;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> s[i];
}
bfs();
system("pause");
return 0;
}