#include <bits/stdc++.h>
using namespace std;
#define maxn 250052
int v,n,m,e;
int ind(int i, int j)
{
return (i-1)*n+j;
}
vector<pair<int,int>> adj[maxn];
int dist[maxn];
bool pos[maxn];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
void dijkstra(int pocetan)
{
//dist[pocetan]=0;
for (int i=0; i<=n*m; ++i){
dist[i]=-1;
}
q.push(make_pair(0, pocetan));
while (!q.empty()){
pair<int,int> x=q.top();
q.pop();
if (!pos[x.second]){
dist[x.second]=x.first;
pos[x.second]=true;
for (auto i:adj[x.second]){
if (!pos[i.first]){
q.push(make_pair(x.first + i.second, i.first));
}
}
}
}
return;
}
int main()
{
cin>>m>>n;
int a[m+1][n+1];
for (int i=1; i<=m; ++i){
string st;
cin>>st;
for (int j=0; j<n; ++j){
if (st[j]=='X'){a[i][j+1]=-1;}
else if (st[j]=='E'){a[i][j+1]=0;}
else if (st[j]=='S'){a[i][j+1]=1;}
else if (st[j]=='W'){a[i][j+1]=2;}
else{a[i][j+1]=3;}
}
}
for (int i=1; i<=m; ++i){
for (int j=1; j<=n; ++j){
if (j<n && a[i][j]>=0){
adj[ind(i,j)].push_back(make_pair(ind(i,j+1), (4-a[i][j])%4));
}
if (j>1 && a[i][j]>=0){
adj[ind(i,j)].push_back(make_pair(ind(i,j-1), (6-a[i][j])%4));
}
if (i>1 && a[i][j]>=0){
adj[ind(i,j)].push_back(make_pair(ind(i-1,j), (7-a[i][j])%4));
}
if (i<m && a[i][j]>=0){
adj[ind(i,j)].push_back(make_pair(ind(i+1,j), (5-a[i][j])%4));
}
}
}
dijkstra(1);
cout<<dist[m*n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7256 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7256 KB |
Output is correct |
7 |
Correct |
2 ms |
7256 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7256 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7256 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7256 KB |
Output is correct |
7 |
Correct |
2 ms |
7256 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7256 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7360 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
2 ms |
7260 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
2 ms |
7260 KB |
Output is correct |
19 |
Correct |
1 ms |
7260 KB |
Output is correct |
20 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7348 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7260 KB |
Output is correct |
7 |
Correct |
2 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7256 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
2 ms |
7256 KB |
Output is correct |
7 |
Correct |
2 ms |
7256 KB |
Output is correct |
8 |
Correct |
1 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7256 KB |
Output is correct |
10 |
Correct |
2 ms |
7260 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7360 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
2 ms |
7260 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
2 ms |
7260 KB |
Output is correct |
19 |
Correct |
1 ms |
7260 KB |
Output is correct |
20 |
Correct |
2 ms |
7260 KB |
Output is correct |
21 |
Correct |
2 ms |
7512 KB |
Output is correct |
22 |
Correct |
2 ms |
7348 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
1 ms |
7260 KB |
Output is correct |
25 |
Correct |
2 ms |
7260 KB |
Output is correct |
26 |
Correct |
2 ms |
7260 KB |
Output is correct |
27 |
Correct |
2 ms |
7260 KB |
Output is correct |
28 |
Correct |
2 ms |
7260 KB |
Output is correct |
29 |
Correct |
2 ms |
7260 KB |
Output is correct |
30 |
Correct |
2 ms |
7260 KB |
Output is correct |
31 |
Correct |
2 ms |
7260 KB |
Output is correct |
32 |
Correct |
2 ms |
7260 KB |
Output is correct |
33 |
Correct |
2 ms |
7260 KB |
Output is correct |
34 |
Correct |
2 ms |
7260 KB |
Output is correct |
35 |
Correct |
9 ms |
8284 KB |
Output is correct |
36 |
Correct |
2 ms |
7256 KB |
Output is correct |
37 |
Correct |
8 ms |
8280 KB |
Output is correct |
38 |
Correct |
6 ms |
9052 KB |
Output is correct |
39 |
Correct |
72 ms |
18204 KB |
Output is correct |
40 |
Correct |
72 ms |
18168 KB |
Output is correct |
41 |
Correct |
29 ms |
17940 KB |
Output is correct |
42 |
Correct |
75 ms |
18144 KB |
Output is correct |
43 |
Correct |
78 ms |
22472 KB |
Output is correct |
44 |
Correct |
29 ms |
17752 KB |
Output is correct |