#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll maxN = 510;
ll n, m, dist[maxN][maxN];
string s[maxN];
ll solve(){
for(int i = 0; i < maxN; i++){
for(int x = 0; x < maxN; x++){
dist[i][x]=-1;
}
}
dist[0][0] = -1;
priority_queue<pair<int, pair<ll, ll>>, vector<pair<int, pair<ll, ll>>>, greater<pair<int, pair<ll, ll>>>> q;
q.push({0, {0, 0}});
while (!q.empty()) {
pair<int, pair<ll, ll>> v = q.top();
q.pop();
ll w = v.first;
ll i = v.second.first;
ll x = v.second.second;
if(i<0||x<0||i>=n||x>=m)continue;
if (dist[i][x]==-1) {
dist[i][x]=w;
if(s[i][x]=='X')continue;
if(s[i][x]=='N'){
q.push({dist[i][x], {i-1, x}});
q.push({dist[i][x]+1, {i, x+1}});
q.push({dist[i][x]+2, {i+1, x}});
q.push({dist[i][x]+3, {i, x-1}});
}
if(s[i][x]=='E'){
q.push({dist[i][x]+3, {i-1, x}});
q.push({dist[i][x], {i, x+1}});
q.push({dist[i][x]+1, {i+1, x}});
q.push({dist[i][x]+2, {i, x-1}});
}
if(s[i][x]=='S'){
q.push({dist[i][x]+2, {i-1, x}});
q.push({dist[i][x]+3, {i, x+1}});
q.push({dist[i][x], {i+1, x}});
q.push({dist[i][x]+1, {i, x-1}});
}
if(s[i][x]=='W'){
q.push({dist[i][x]+1, {i-1, x}});
q.push({dist[i][x]+2, {i, x+1}});
q.push({dist[i][x]+3, {i+1, x}});
q.push({dist[i][x], {i, x-1}});
}
}
}
/*for(int i = 0; i < 4; i++){
for(int x = 0; x < 4; x++){
cout<<dist[i][x]<<" ";
}
cout<<endl;
}*/
return dist[n-1][m-1];
}
int main()
{
cin>>n>>m;
for(int i = 0; i < n; i++){
cin>>s[i];
}
cout<<solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2332 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2372 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2244 KB |
Output is correct |
8 |
Correct |
1 ms |
2260 KB |
Output is correct |
9 |
Correct |
1 ms |
2260 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2332 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2372 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2244 KB |
Output is correct |
8 |
Correct |
1 ms |
2260 KB |
Output is correct |
9 |
Correct |
1 ms |
2260 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
2240 KB |
Output is correct |
12 |
Correct |
1 ms |
2260 KB |
Output is correct |
13 |
Correct |
1 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
1 ms |
2260 KB |
Output is correct |
17 |
Correct |
1 ms |
2260 KB |
Output is correct |
18 |
Correct |
1 ms |
2244 KB |
Output is correct |
19 |
Correct |
1 ms |
2260 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2260 KB |
Output is correct |
2 |
Correct |
1 ms |
2248 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
1 ms |
2260 KB |
Output is correct |
8 |
Correct |
1 ms |
2252 KB |
Output is correct |
9 |
Correct |
1 ms |
2260 KB |
Output is correct |
10 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2332 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2372 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2260 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2244 KB |
Output is correct |
8 |
Correct |
1 ms |
2260 KB |
Output is correct |
9 |
Correct |
1 ms |
2260 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
2240 KB |
Output is correct |
12 |
Correct |
1 ms |
2260 KB |
Output is correct |
13 |
Correct |
1 ms |
2260 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
1 ms |
2260 KB |
Output is correct |
17 |
Correct |
1 ms |
2260 KB |
Output is correct |
18 |
Correct |
1 ms |
2244 KB |
Output is correct |
19 |
Correct |
1 ms |
2260 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2260 KB |
Output is correct |
22 |
Correct |
1 ms |
2260 KB |
Output is correct |
23 |
Correct |
1 ms |
2260 KB |
Output is correct |
24 |
Correct |
1 ms |
2260 KB |
Output is correct |
25 |
Correct |
1 ms |
2260 KB |
Output is correct |
26 |
Correct |
1 ms |
2248 KB |
Output is correct |
27 |
Correct |
1 ms |
2260 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2260 KB |
Output is correct |
32 |
Correct |
1 ms |
2252 KB |
Output is correct |
33 |
Correct |
1 ms |
2260 KB |
Output is correct |
34 |
Correct |
2 ms |
2260 KB |
Output is correct |
35 |
Correct |
12 ms |
2388 KB |
Output is correct |
36 |
Correct |
1 ms |
2260 KB |
Output is correct |
37 |
Correct |
14 ms |
2476 KB |
Output is correct |
38 |
Correct |
2 ms |
2388 KB |
Output is correct |
39 |
Correct |
129 ms |
3284 KB |
Output is correct |
40 |
Correct |
131 ms |
3236 KB |
Output is correct |
41 |
Correct |
7 ms |
2984 KB |
Output is correct |
42 |
Correct |
132 ms |
3288 KB |
Output is correct |
43 |
Correct |
202 ms |
27812 KB |
Output is correct |
44 |
Correct |
10 ms |
3020 KB |
Output is correct |