#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
using namespace std;
typedef int ll;
ll dp[507][507], used[507][507];
string a[507];
char rotate(char c){
if(c == 'N')
return 'E';
if(c == 'E')
return 'S';
if(c == 'S')
return 'W';
return 'N';
}
pair<ll, ll> move(ll &r, ll &c, char &v){
if(v == 'N')
return {r - 1, c};
if(v == 'E')
return {r, c + 1};
if(v == 'S')
return {r + 1, c};
return {r, c - 1};
}
ll n, m;
void count_dp(ll &r, ll &c, ll step){
if(r < 1 || c < 1 || r > n || c > m || dp[r][c] <= step){
return;
}
if(a[r][c] == 'X'){
if(r == n && c == m)
dp[r][c] = step;
return;
}
dp[r][c] = step;
char v = a[r][c];
for(int i = 0; i < 4; i++){
auto y = move(r, c, v);
if(y.first >= 1 && y.second >= 1 && y.first <= n && y.second <= m && dp[y.first][y.second] > step + i)
count_dp(y.first, y.second, step + i);
v = rotate(v);
}
}
ll cost(char a, char b){
for(int i = 0; i < 4; i++){
if(a == b)
return i;
a = rotate(a);
}
return 0;
}
struct one{
ll r, c, cost;
};
bool operator < (const one &p1, const one &p2){
return p1.cost < p2.cost;
}
bool operator > (const one &p1, const one &p2){
return p1.cost > p2.cost;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
a[i] = "#" + a[i];
}
priority_queue<one, vector<one>, greater<one>> q;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j] = 1e9;
dp[1][1] = 0;
q.push({1, 1, 0});
while(!q.empty()){
auto v = q.top();
q.pop();
if(v.r == n && v.c == m){
break;
}
if(used[v.r][v.c])
continue;
used[v.r][v.c] = 1;
if(a[v.r][v.c] == 'X')
continue;
ll r = v.r, c = v.c;
if(r - 1 >= 1 && cost(a[r][c], 'N') + v.cost < dp[r - 1][c]){
dp[r - 1][c] = cost(a[r][c], 'N') + v.cost;
q.push({r - 1, c, dp[r - 1][c]});
}
if(r + 1 <= n && cost(a[r][c], 'S') + v.cost < dp[r + 1][c]){
dp[r + 1][c] = cost(a[r][c], 'S') + v.cost;
q.push({r + 1, c, dp[r + 1][c]});
}
if(c - 1 >= 1 && cost(a[r][c], 'W') + v.cost < dp[r][c - 1]){
dp[r][c - 1] = cost(a[r][c], 'W') + v.cost;
q.push({r, c - 1, dp[r][c - 1]});
}
if(c + 1 <= m && cost(a[r][c], 'E') + v.cost < dp[r][c + 1]){
dp[r][c + 1] = cost(a[r][c], 'E') + v.cost;
q.push({r, c + 1, dp[r][c + 1]});
}
}
if(dp[n][m] >= (ll)(1e9))
dp[n][m] = -1;
cout << dp[n][m] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
4 ms |
720 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
37 ms |
2760 KB |
Output is correct |
40 |
Correct |
36 ms |
2840 KB |
Output is correct |
41 |
Correct |
6 ms |
1704 KB |
Output is correct |
42 |
Correct |
40 ms |
2780 KB |
Output is correct |
43 |
Correct |
46 ms |
5720 KB |
Output is correct |
44 |
Correct |
6 ms |
1748 KB |
Output is correct |