#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define f first
#define s second
#define pll pair<ll, ll>
priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>q;
const ll sz=505, mod=1e9+7;
map<char, ll>mp;
ll n, m, used[sz][sz], dis[sz][sz];
char k[sz][sz];
vector<pair<pair<ll, ll>, ll>>g[sz][sz];
ll cnt(char a, char b)
{
ll dif=mp[a]-mp[b];
if(dif<0) dif+=4;
return dif;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
mp['E']=0;
mp['N']=1;
mp['W']=2;
mp['S']=3;
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>k[i][j];
if(k[i][j]=='X') continue;
if(i>1){
g[i][j].push_back({{i-1, j}, cnt(k[i][j], 'N')});
}
if(i<n){
g[i][j].push_back({{i+1, j}, cnt(k[i][j], 'S')});
}
if(j>1){
g[i][j].push_back({{i, j-1}, cnt(k[i][j], 'W')});
}
if(j<m){
g[i][j].push_back({{i, j+1}, cnt(k[i][j], 'E')});
}
}
}
memset(dis, 32, sizeof(dis));
q.push({0, {1, 1}});
dis[1][1]=0;
while(q.size()){
ll x=q.top().s.first;
ll y=q.top().s.second;
q.pop();
if(used[x][y]) continue;
used[x][y]=1;
for(auto j:g[x][y]){
ll u=j.f.f, v=j.f.s, w=j.s;
if(dis[u][v]>dis[x][y]+w){
dis[u][v]=dis[x][y]+w;
q.push({dis[u][v], {u, v}});
}
}
}
if(!used[n][m]) cout<<-1;
else cout<<dis[n][m];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8840 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8856 KB |
Output is correct |
17 |
Correct |
1 ms |
8792 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
2 ms |
8836 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8856 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8840 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8856 KB |
Output is correct |
17 |
Correct |
1 ms |
8792 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
1 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
2 ms |
8836 KB |
Output is correct |
31 |
Correct |
1 ms |
8796 KB |
Output is correct |
32 |
Correct |
1 ms |
8856 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
35 |
Correct |
9 ms |
10844 KB |
Output is correct |
36 |
Correct |
1 ms |
8796 KB |
Output is correct |
37 |
Correct |
9 ms |
11356 KB |
Output is correct |
38 |
Correct |
7 ms |
12452 KB |
Output is correct |
39 |
Correct |
80 ms |
32840 KB |
Output is correct |
40 |
Correct |
82 ms |
32848 KB |
Output is correct |
41 |
Correct |
32 ms |
30804 KB |
Output is correct |
42 |
Correct |
80 ms |
32852 KB |
Output is correct |
43 |
Correct |
93 ms |
44140 KB |
Output is correct |
44 |
Correct |
32 ms |
30808 KB |
Output is correct |