#include <bits/stdc++.h>
using namespace std;
int n, m, val;
vector<vector<vector<pair<pair<int, int>, int> > > > g(502, vector<vector<pair<pair<int, int>, int> > > (502));
char ch;
bool done[502][502];
int dis[502][502];
struct node {
int x, y;
int dist;
bool operator <(const node& other) const {
return dist > other.dist;
}
};
node n1, n2;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
cin >> ch;
if(ch == 'X') continue;
if(ch == 'N') val = 0;
if(ch == 'E') val = 1;
if(ch == 'S') val = 2;
if(ch == 'W') val = 3;
g[i][j].push_back(make_pair(make_pair(i-1, j), (-val+4+0)%4));
g[i][j].push_back(make_pair(make_pair(i, j+1), (-val+4+1)%4));
g[i][j].push_back(make_pair(make_pair(i+1, j), (-val+4+2)%4));
g[i][j].push_back(make_pair(make_pair(i, j-1), (-val+4+3)%4));
/*for(auto p : g[i][j]) {
cout << i << " " << j << " -> " << p.first.first << " " << p.first.second << ": " << p.second << "\n";
}*/
}
}
for(int i = 0; i <= n+1; ++i) {
for(int j = 0; j <= m+1; ++j) {
dis[i][j] = -1;
}
}
priority_queue<node> q;
n1.x = 1;
n1.y = 1;
n1.dist = 0;
dis[n1.x][n1.y] = 0;
q.push(n1);
while(!q.empty()) {
n1 = q.top();
q.pop();
if(!done[n1.x][n1.y]) {
done[n1.x][n1.y] = true;
for(auto u : g[n1.x][n1.y]) {
if(!done[u.first.first][u.first.second]) {
if((dis[u.first.first][u.first.second] == -1) || (dis[u.first.first][u.first.second] > dis[n1.x][n1.y]+u.second)) {
n2.x = u.first.first;
n2.y = u.first.second;
n2.dist = dis[n1.x][n1.y] + u.second;
dis[n2.x][n2.y] = n2.dist;
q.push(n2);
}
}
}
}
}
cout << dis[n][m] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
5 ms |
6220 KB |
Output is correct |
3 |
Correct |
5 ms |
6268 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6276 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
5 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
5 ms |
6220 KB |
Output is correct |
3 |
Correct |
5 ms |
6268 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6276 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
5 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
4 ms |
6268 KB |
Output is correct |
12 |
Correct |
4 ms |
6284 KB |
Output is correct |
13 |
Correct |
5 ms |
6272 KB |
Output is correct |
14 |
Correct |
4 ms |
6220 KB |
Output is correct |
15 |
Correct |
5 ms |
6272 KB |
Output is correct |
16 |
Correct |
5 ms |
6220 KB |
Output is correct |
17 |
Correct |
5 ms |
6220 KB |
Output is correct |
18 |
Correct |
4 ms |
6220 KB |
Output is correct |
19 |
Correct |
5 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
5 ms |
6220 KB |
Output is correct |
3 |
Correct |
5 ms |
6220 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
5 ms |
6276 KB |
Output is correct |
3 |
Correct |
5 ms |
6220 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
6 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6208 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
6 ms |
6220 KB |
Output is correct |
10 |
Correct |
5 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6220 KB |
Output is correct |
2 |
Correct |
5 ms |
6220 KB |
Output is correct |
3 |
Correct |
5 ms |
6268 KB |
Output is correct |
4 |
Correct |
5 ms |
6220 KB |
Output is correct |
5 |
Correct |
5 ms |
6220 KB |
Output is correct |
6 |
Correct |
5 ms |
6220 KB |
Output is correct |
7 |
Correct |
5 ms |
6276 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
5 ms |
6220 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
11 |
Correct |
4 ms |
6268 KB |
Output is correct |
12 |
Correct |
4 ms |
6284 KB |
Output is correct |
13 |
Correct |
5 ms |
6272 KB |
Output is correct |
14 |
Correct |
4 ms |
6220 KB |
Output is correct |
15 |
Correct |
5 ms |
6272 KB |
Output is correct |
16 |
Correct |
5 ms |
6220 KB |
Output is correct |
17 |
Correct |
5 ms |
6220 KB |
Output is correct |
18 |
Correct |
4 ms |
6220 KB |
Output is correct |
19 |
Correct |
5 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
5 ms |
6220 KB |
Output is correct |
22 |
Correct |
5 ms |
6220 KB |
Output is correct |
23 |
Correct |
5 ms |
6220 KB |
Output is correct |
24 |
Correct |
5 ms |
6220 KB |
Output is correct |
25 |
Correct |
5 ms |
6220 KB |
Output is correct |
26 |
Correct |
5 ms |
6276 KB |
Output is correct |
27 |
Correct |
5 ms |
6220 KB |
Output is correct |
28 |
Correct |
5 ms |
6220 KB |
Output is correct |
29 |
Correct |
5 ms |
6220 KB |
Output is correct |
30 |
Correct |
6 ms |
6220 KB |
Output is correct |
31 |
Correct |
5 ms |
6208 KB |
Output is correct |
32 |
Correct |
5 ms |
6220 KB |
Output is correct |
33 |
Correct |
6 ms |
6220 KB |
Output is correct |
34 |
Correct |
5 ms |
6220 KB |
Output is correct |
35 |
Correct |
11 ms |
7500 KB |
Output is correct |
36 |
Correct |
5 ms |
6400 KB |
Output is correct |
37 |
Correct |
13 ms |
7888 KB |
Output is correct |
38 |
Correct |
11 ms |
8900 KB |
Output is correct |
39 |
Correct |
93 ms |
20328 KB |
Output is correct |
40 |
Correct |
93 ms |
20288 KB |
Output is correct |
41 |
Correct |
43 ms |
19908 KB |
Output is correct |
42 |
Correct |
94 ms |
20252 KB |
Output is correct |
43 |
Correct |
121 ms |
26432 KB |
Output is correct |
44 |
Correct |
43 ms |
19992 KB |
Output is correct |