#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 500*500+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
vector<vector<pii>> graf(MAXN);
int n,m;
int calc(char a)
{
if(a == 'E')return 1;
if(a == 'S')return 2;
if(a == 'W')return 3;
if(a == 'N')return 4;
return INT_MAX;
}
int f(char a, char b)
{
int t =(calc(b) - calc(a));
if(t<0)return 4+t;
else return t;
}
set<pii> que;
vector<int> dist(MAXN, inf);
void dikstra(int nod)
{
dist[nod] = 0;
que.insert(mp(0, nod));
while(!que.empty())
{
auto it = que.begin();
int u = it -> second;
que.erase(it);
for(auto x: graf[u])
{
int v = x.fi; int w = x.sc;
if(dist[v] > dist[u]+w)
{
que.erase({dist[v], v});
dist[v] = dist[u] + w;
que.insert({dist[v], v});
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
cin >> n >> m;
vector<vector<char>> mat(n, vector<char>(m));
for(int i=0; i<n; i++)
{
string s;cin >> s;
for(int j=0; j<m;j++)
{
mat[i][j] = s[j];
if(calc(s[j]) == INT_MAX)continue;
if(i!=n-1)graf[i*m+j].pb(mp( (i+1)*m+j,f(mat[i][j], 'S')));
if(i!=0)graf[i*m+j].pb(mp((i-1)*m+j,f(mat[i][j], 'N')));
if(j!=0)graf[i*m+j].pb(mp(i*m+j-1,f(mat[i][j], 'W')));
if(j!=m-1)graf[i*m+j].pb(mp(i*m+j+1,f(mat[i][j], 'E')));
}
}
for(int i=0; i<n; i++)
dikstra(0);
if(dist[n*m-1] == inf)cout << -1 << endl;
else cout << dist[n*m-1] << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8136 KB |
Output is correct |
3 |
Correct |
3 ms |
8280 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
4 ms |
8288 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8280 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8136 KB |
Output is correct |
3 |
Correct |
3 ms |
8280 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
4 ms |
8288 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8280 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
8068 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
4 ms |
8300 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
4 ms |
8284 KB |
Output is correct |
6 |
Correct |
4 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8108 KB |
Output is correct |
9 |
Correct |
3 ms |
8280 KB |
Output is correct |
10 |
Correct |
3 ms |
8296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8136 KB |
Output is correct |
3 |
Correct |
3 ms |
8280 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
4 ms |
8288 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8280 KB |
Output is correct |
8 |
Correct |
4 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8280 KB |
Output is correct |
10 |
Correct |
3 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
3 ms |
8284 KB |
Output is correct |
14 |
Correct |
3 ms |
8284 KB |
Output is correct |
15 |
Correct |
3 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
3 ms |
8284 KB |
Output is correct |
20 |
Correct |
3 ms |
8284 KB |
Output is correct |
21 |
Correct |
3 ms |
8280 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
4 ms |
8068 KB |
Output is correct |
24 |
Correct |
3 ms |
8284 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
8300 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
4 ms |
8284 KB |
Output is correct |
30 |
Correct |
4 ms |
8284 KB |
Output is correct |
31 |
Correct |
3 ms |
8284 KB |
Output is correct |
32 |
Correct |
3 ms |
8108 KB |
Output is correct |
33 |
Correct |
3 ms |
8280 KB |
Output is correct |
34 |
Correct |
3 ms |
8296 KB |
Output is correct |
35 |
Correct |
9 ms |
9564 KB |
Output is correct |
36 |
Correct |
4 ms |
8280 KB |
Output is correct |
37 |
Correct |
9 ms |
9816 KB |
Output is correct |
38 |
Correct |
6 ms |
10844 KB |
Output is correct |
39 |
Correct |
69 ms |
24476 KB |
Output is correct |
40 |
Correct |
69 ms |
24408 KB |
Output is correct |
41 |
Correct |
26 ms |
24408 KB |
Output is correct |
42 |
Correct |
73 ms |
24408 KB |
Output is correct |
43 |
Correct |
63 ms |
28240 KB |
Output is correct |
44 |
Correct |
25 ms |
24412 KB |
Output is correct |