#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define pll pair<long long, long long>
using namespace std;
ll mv_x[] = {-1,0,1,0};
ll mv_y[] = {0,1,0,-1};
map<char,ll> mp;
ll n,m;
ll arr[1000][1000];
ll visited[1000][1000];
void dijkstra(){
using plll = pair<ll,pll>;
priority_queue<plll, vector<plll>, greater<plll>> pq;
pq.push({0,{1,1}});
while(!pq.empty()){
ll cost = pq.top().fi;
ll x = pq.top().se.fi;
ll y = pq.top().se.se;
pq.pop();
if(visited[x][y]!=-1) continue;
visited[x][y] = cost;
if(x==n&&y==m) break;
if(arr[x][y]==-1) continue;
for(int i=0;i<=3;i++){
ll idx = (arr[x][y] + i)%4;
ll new_x = x + mv_x[idx];
ll new_y = y + mv_y[idx];
if(visited[new_x][new_y]!=-1) continue;
if(new_x<1||new_x>n) continue;
if(new_y<1||new_y>m) continue;
pq.push({cost+i,{new_x,new_y}});
}
}
}
void solve(){
cin >> n >> m;
mp['N'] = 0;
mp['E'] = 1;
mp['S'] = 2;
mp['W'] = 3;
mp['X'] = -1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char c;
cin >> c;
arr[i][j] = mp[c];
}
}
memset(visited,-1,sizeof visited);
dijkstra();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++){
// cout << visited[i][j] << ' ';
// }
// cout << endl;
// }
cout << visited[n][m] << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tc = 1;
// cin >> tc;
while(tc--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 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 |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 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 |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
9048 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
9044 KB |
Output is correct |
7 |
Correct |
2 ms |
9048 KB |
Output is correct |
8 |
Correct |
2 ms |
9048 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8792 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
2 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 |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
9048 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8792 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 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 |
1 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
9044 KB |
Output is correct |
31 |
Correct |
2 ms |
9048 KB |
Output is correct |
32 |
Correct |
2 ms |
9048 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8792 KB |
Output is correct |
35 |
Correct |
7 ms |
8792 KB |
Output is correct |
36 |
Correct |
2 ms |
8796 KB |
Output is correct |
37 |
Correct |
9 ms |
11100 KB |
Output is correct |
38 |
Correct |
3 ms |
10844 KB |
Output is correct |
39 |
Correct |
62 ms |
13028 KB |
Output is correct |
40 |
Correct |
62 ms |
13028 KB |
Output is correct |
41 |
Correct |
8 ms |
12888 KB |
Output is correct |
42 |
Correct |
62 ms |
13024 KB |
Output is correct |
43 |
Correct |
45 ms |
20928 KB |
Output is correct |
44 |
Correct |
8 ms |
12888 KB |
Output is correct |