#include<bits/stdc++.h>
using namespace std;
const int N = 505, INF = 1e9 + 5;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m;
char a[N][N];
vector<pair<pair<int, int>, int>> g[N][N];
int dist[N][N];
inline bool check(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
map<char, int> mp;
mp['N'] = 0;
mp['E'] = 1;
mp['S'] = 2;
mp['W'] = 3;
cin >> n >> m;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> a[i][j];
if(a[i][j] != 'X'){
for(int k = 0; k < 4; k++){
int dst = k - mp[a[i][j]];
if(dst < 0){
dst += 4;
}
int x = i + dx[k], y = j + dy[k];
if(check(x, y)){
g[i][j].push_back({{x, y}, dst});
}
}
}
dist[i][j] = INF;
}
}
dist[0][0] = 0;
set<pair<int, pair<int, int>>> st = {{0, {0, 0}}};
while(!st.empty()){
auto v = st.begin()->second;
st.erase(st.begin());
for(auto to : g[v.first][v.second]){
int dst = dist[v.first][v.second] + to.second;
if(dst < dist[to.first.first][to.first.second]){
st.erase({dist[to.first.first][to.first.second], to.first});
dist[to.first.first][to.first.second] = dst;
st.insert({dist[to.first.first][to.first.second], to.first});
}
}
}
if(dist[n - 1][m - 1] == INF){
cout << -1;
} else{
cout << dist[n - 1][m - 1];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
9 ms |
6400 KB |
Output is correct |
4 |
Correct |
9 ms |
6400 KB |
Output is correct |
5 |
Correct |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
9 ms |
6400 KB |
Output is correct |
4 |
Correct |
9 ms |
6400 KB |
Output is correct |
5 |
Correct |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
11 |
Correct |
8 ms |
6272 KB |
Output is correct |
12 |
Correct |
8 ms |
6400 KB |
Output is correct |
13 |
Correct |
5 ms |
6400 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6272 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
9 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
10 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6272 KB |
Output is correct |
2 |
Correct |
8 ms |
6272 KB |
Output is correct |
3 |
Correct |
9 ms |
6272 KB |
Output is correct |
4 |
Correct |
8 ms |
6272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
8 ms |
6400 KB |
Output is correct |
4 |
Correct |
8 ms |
6400 KB |
Output is correct |
5 |
Correct |
9 ms |
6400 KB |
Output is correct |
6 |
Correct |
9 ms |
6528 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
9 ms |
6528 KB |
Output is correct |
10 |
Correct |
9 ms |
6400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6400 KB |
Output is correct |
2 |
Correct |
8 ms |
6400 KB |
Output is correct |
3 |
Correct |
9 ms |
6400 KB |
Output is correct |
4 |
Correct |
9 ms |
6400 KB |
Output is correct |
5 |
Correct |
8 ms |
6400 KB |
Output is correct |
6 |
Correct |
9 ms |
6400 KB |
Output is correct |
7 |
Correct |
8 ms |
6400 KB |
Output is correct |
8 |
Correct |
8 ms |
6400 KB |
Output is correct |
9 |
Correct |
8 ms |
6400 KB |
Output is correct |
10 |
Correct |
8 ms |
6272 KB |
Output is correct |
11 |
Correct |
8 ms |
6272 KB |
Output is correct |
12 |
Correct |
8 ms |
6400 KB |
Output is correct |
13 |
Correct |
5 ms |
6400 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6272 KB |
Output is correct |
16 |
Correct |
8 ms |
6272 KB |
Output is correct |
17 |
Correct |
9 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6272 KB |
Output is correct |
19 |
Correct |
8 ms |
6400 KB |
Output is correct |
20 |
Correct |
10 ms |
6400 KB |
Output is correct |
21 |
Correct |
8 ms |
6272 KB |
Output is correct |
22 |
Correct |
8 ms |
6272 KB |
Output is correct |
23 |
Correct |
9 ms |
6272 KB |
Output is correct |
24 |
Correct |
8 ms |
6272 KB |
Output is correct |
25 |
Correct |
9 ms |
6400 KB |
Output is correct |
26 |
Correct |
8 ms |
6400 KB |
Output is correct |
27 |
Correct |
8 ms |
6400 KB |
Output is correct |
28 |
Correct |
8 ms |
6400 KB |
Output is correct |
29 |
Correct |
9 ms |
6400 KB |
Output is correct |
30 |
Correct |
9 ms |
6528 KB |
Output is correct |
31 |
Correct |
8 ms |
6400 KB |
Output is correct |
32 |
Correct |
8 ms |
6400 KB |
Output is correct |
33 |
Correct |
9 ms |
6528 KB |
Output is correct |
34 |
Correct |
9 ms |
6400 KB |
Output is correct |
35 |
Correct |
18 ms |
7680 KB |
Output is correct |
36 |
Correct |
9 ms |
6656 KB |
Output is correct |
37 |
Correct |
21 ms |
7936 KB |
Output is correct |
38 |
Correct |
17 ms |
9216 KB |
Output is correct |
39 |
Correct |
135 ms |
20472 KB |
Output is correct |
40 |
Correct |
130 ms |
20344 KB |
Output is correct |
41 |
Correct |
59 ms |
20344 KB |
Output is correct |
42 |
Correct |
129 ms |
20472 KB |
Output is correct |
43 |
Correct |
133 ms |
23416 KB |
Output is correct |
44 |
Correct |
58 ms |
20344 KB |
Output is correct |