#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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
1 ms |
8808 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8896 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8908 KB |
Output is correct |
6 |
Correct |
2 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 |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8792 KB |
Output is correct |
16 |
Correct |
1 ms |
8808 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 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 |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8896 KB |
Output is correct |
28 |
Correct |
2 ms |
8792 KB |
Output is correct |
29 |
Correct |
2 ms |
8908 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
1 ms |
8796 KB |
Output is correct |
32 |
Correct |
1 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
35 |
Correct |
8 ms |
10840 KB |
Output is correct |
36 |
Correct |
2 ms |
8796 KB |
Output is correct |
37 |
Correct |
9 ms |
11292 KB |
Output is correct |
38 |
Correct |
6 ms |
12228 KB |
Output is correct |
39 |
Correct |
77 ms |
32460 KB |
Output is correct |
40 |
Correct |
78 ms |
32596 KB |
Output is correct |
41 |
Correct |
32 ms |
30556 KB |
Output is correct |
42 |
Correct |
83 ms |
32564 KB |
Output is correct |
43 |
Correct |
91 ms |
43704 KB |
Output is correct |
44 |
Correct |
32 ms |
30560 KB |
Output is correct |