#include <bits/stdc++.h>
#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\n'
using namespace std;
const int MOD = 1e9 + 7;
const int N = 510;
int n, m, dis[N][N], used[N][N];
char g[N][N];
map<char, int> mp;
int e(char a, char b) {
if (mp[a] > mp[b]) {
return (4 - mp[a]) + mp[b];
} else return mp[b] - mp[a];
}
void bfs(int x, int y) {
used[x][y] = 1;
dis[x][y] = 0;
queue<pair<int, int>> q;
q.push({x, y});
while(!q.empty()) {
pair<int, int> u = q.front();
x = u.F, y = u.S;
used[x][y] = 1;
if ( x ) { // up
if ( g[x-1][y] == 'S' and dis[x-1][y] > dis[x][y]) {
dis[x-1][y] = dis[x][y];
q.push({x-1,y});
} else if ( g[x-1][y] != 'X' and dis[x-1][y] > dis[x][y] + e(g[x-1][y],'S')) {
dis[x-1][y] = dis[x][y] + e(g[x-1][y], 'S');
q.push({x-1,y});
}
} if ( y ) { // left
if ( g[x][y-1] == 'E' and dis[x][y-1] > dis[x][y]) {
dis[x][y-1] = dis[x][y];
q.push({x,y-1});
} else if ( g[x][y-1] != 'X' and dis[x][y-1] > dis[x][y] + e(g[x][y-1],'E')) {
dis[x][y-1] = dis[x][y] + e(g[x][y-1], 'E');
q.push({x,y-1});
}
} if ( x < n - 1) { //down
if ( g[x+1][y] == 'N' and dis[x+1][y] > dis[x][y]) {
dis[x+1][y] = dis[x][y];
q.push({x+1,y});
} else if ( g[x+1][y] != 'X' and dis[x+1][y] > dis[x][y] + e(g[x+1][y],'N')) {
dis[x+1][y] = dis[x][y] + e(g[x+1][y], 'N');
q.push({x+1,y});
}
} if (y < m - 1) {
if ( g[x][y+1] == 'W' and dis[x][y+1] > dis[x][y]) {
dis[x][y+1] = dis[x][y];
q.push({x,y+1});
} else if ( g[x][y+1] != 'X' and dis[x][y+1] > dis[x][y] + e(g[x][y+1],'W')) {
dis[x][y+1] = dis[x][y] + e(g[x][y+1], 'W');
q.push({x,y+1});
}
}
q.pop();
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
mp['N'] = 1;
mp['E'] = 2;
mp['S'] = 3;
mp['W'] = 4;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
dis[i][j] = INT_MAX;
cin >> g[i][j];
}
}
bfs(n-1,m-1);
if ( dis[0][0] == INT_MAX)
cout << "-1\n";
else
cout << dis[0][0] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
0 ms |
384 KB |
Output is correct |
26 |
Correct |
0 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
7 ms |
384 KB |
Output is correct |
37 |
Correct |
55 ms |
896 KB |
Output is correct |
38 |
Correct |
103 ms |
1648 KB |
Output is correct |
39 |
Correct |
1305 ms |
2964 KB |
Output is correct |
40 |
Correct |
1226 ms |
3064 KB |
Output is correct |
41 |
Correct |
1330 ms |
3084 KB |
Output is correct |
42 |
Correct |
1361 ms |
3064 KB |
Output is correct |
43 |
Execution timed out |
2084 ms |
2808 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |