#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define p push
#define pb push_back
#define F(end) for(int i=0; i<end; i++)
#define ll long long
int m, n; const int inf = 1e9;
const int maxn = 1e6;
bool vis[maxn]; ll dist[maxn];
vector<pii> adj[maxn];
priority_queue<pii,vector<pii>, greater<pii>> q;
int pirmine[maxn];
void dijkstra(int x){
for(int i=0; i<m*n; i++){
vis[i] = false; dist[i] = inf; pirmine[i] =-1;
}
dist[x] = 0;
q.p({dist[x], x});
while(!q.empty()){
int v = q.top().se; q.pop();
if(vis[v])continue;
vis[v] = true;
for(auto u: adj[v]){
int j = u.fi; int w = u.se;
if(dist[v] +w < dist[j]){
dist[j] = dist[v] +w;
pirmine[j] = v;
q.p({dist[j], j});
}
}
}
}
int main()
{
cin>>m>>n; string s[m];
F(m)cin>>s[i];
F(m){
for(int a=0; a<n-1; a++){
if(s[i][a] == 'E')adj[a + i*n].pb({a+i*n +1, 0});
if(s[i][a] == 'N')adj[a + i*n].pb({a+i*n +1, 1});
if(s[i][a] == 'W')adj[a + i*n].pb({a+i*n +1, 2});
if(s[i][a] == 'S')adj[a + i*n].pb({a+i*n +1, 3});
}
}
F(m){
for(int a=1; a<n; a++){
if(s[i][a] == 'W')adj[a + i*n].pb({a+i*n -1, 0});
if(s[i][a] == 'S')adj[a + i*n].pb({a+i*n -1, 1});
if(s[i][a] == 'E')adj[a + i*n].pb({a+i*n -1, 2});
if(s[i][a] == 'N')adj[a + i*n].pb({a+i*n -1, 3});
}
}
F(n){
for(int a=0; a<m-1; a++){
if(s[a][i] == 'S')adj[a*n + i].pb({(a+1)*n + i, 0});
if(s[a][i] == 'E')adj[a*n + i].pb({(a+1)*n + i, 1});
if(s[a][i] == 'N')adj[a*n + i].pb({(a+1)*n + i, 2});
if(s[a][i] == 'W')adj[a*n + i].pb({(a+1)*n + i, 3});
}
}
F(n){
for(int a=1; a<m; a++){
if(s[a][i] == 'N')adj[a*n + i].pb({(a-1)*n + i, 0});
if(s[a][i] == 'W')adj[a*n + i].pb({(a-1)*n + i, 1});
if(s[a][i] == 'S')adj[a*n + i].pb({(a-1)*n + i, 2});
if(s[a][i] == 'E')adj[a*n + i].pb({(a-1)*n + i, 3});
}
}
dijkstra(0);
int d = pirmine[n*m-1]; //cout<<d<<endl;
//for(auto i: adj[0])cout<<i.fi<<i.se<<endl;
//cout<<dist[4];
if(dist[n*m-1] == inf){
cout<<-1;
}
else{
cout<<dist[n*m-1]<<endl;
}
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:80:10: warning: unused variable 'd' [-Wunused-variable]
80 | int d = pirmine[n*m-1]; //cout<<d<<endl;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23776 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23704 KB |
Output is correct |
6 |
Correct |
14 ms |
23700 KB |
Output is correct |
7 |
Correct |
14 ms |
23716 KB |
Output is correct |
8 |
Correct |
13 ms |
23768 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23776 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23704 KB |
Output is correct |
6 |
Correct |
14 ms |
23700 KB |
Output is correct |
7 |
Correct |
14 ms |
23716 KB |
Output is correct |
8 |
Correct |
13 ms |
23768 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
13 ms |
23756 KB |
Output is correct |
12 |
Correct |
14 ms |
23728 KB |
Output is correct |
13 |
Correct |
16 ms |
23792 KB |
Output is correct |
14 |
Correct |
13 ms |
23756 KB |
Output is correct |
15 |
Correct |
14 ms |
23784 KB |
Output is correct |
16 |
Correct |
15 ms |
23800 KB |
Output is correct |
17 |
Correct |
13 ms |
23756 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
13 ms |
23724 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23680 KB |
Output is correct |
2 |
Correct |
13 ms |
23772 KB |
Output is correct |
3 |
Correct |
13 ms |
23760 KB |
Output is correct |
4 |
Correct |
13 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23868 KB |
Output is correct |
2 |
Correct |
16 ms |
23804 KB |
Output is correct |
3 |
Correct |
14 ms |
23748 KB |
Output is correct |
4 |
Correct |
15 ms |
23800 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23796 KB |
Output is correct |
8 |
Correct |
15 ms |
23796 KB |
Output is correct |
9 |
Correct |
14 ms |
23744 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23776 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
16 ms |
23704 KB |
Output is correct |
6 |
Correct |
14 ms |
23700 KB |
Output is correct |
7 |
Correct |
14 ms |
23716 KB |
Output is correct |
8 |
Correct |
13 ms |
23768 KB |
Output is correct |
9 |
Correct |
14 ms |
23780 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
13 ms |
23756 KB |
Output is correct |
12 |
Correct |
14 ms |
23728 KB |
Output is correct |
13 |
Correct |
16 ms |
23792 KB |
Output is correct |
14 |
Correct |
13 ms |
23756 KB |
Output is correct |
15 |
Correct |
14 ms |
23784 KB |
Output is correct |
16 |
Correct |
15 ms |
23800 KB |
Output is correct |
17 |
Correct |
13 ms |
23756 KB |
Output is correct |
18 |
Correct |
13 ms |
23756 KB |
Output is correct |
19 |
Correct |
13 ms |
23724 KB |
Output is correct |
20 |
Correct |
13 ms |
23820 KB |
Output is correct |
21 |
Correct |
16 ms |
23680 KB |
Output is correct |
22 |
Correct |
13 ms |
23772 KB |
Output is correct |
23 |
Correct |
13 ms |
23760 KB |
Output is correct |
24 |
Correct |
13 ms |
23756 KB |
Output is correct |
25 |
Correct |
14 ms |
23868 KB |
Output is correct |
26 |
Correct |
16 ms |
23804 KB |
Output is correct |
27 |
Correct |
14 ms |
23748 KB |
Output is correct |
28 |
Correct |
15 ms |
23800 KB |
Output is correct |
29 |
Correct |
15 ms |
23756 KB |
Output is correct |
30 |
Correct |
14 ms |
23756 KB |
Output is correct |
31 |
Correct |
14 ms |
23796 KB |
Output is correct |
32 |
Correct |
15 ms |
23796 KB |
Output is correct |
33 |
Correct |
14 ms |
23744 KB |
Output is correct |
34 |
Correct |
14 ms |
23756 KB |
Output is correct |
35 |
Correct |
23 ms |
25044 KB |
Output is correct |
36 |
Correct |
15 ms |
23872 KB |
Output is correct |
37 |
Correct |
24 ms |
25164 KB |
Output is correct |
38 |
Correct |
29 ms |
26060 KB |
Output is correct |
39 |
Correct |
139 ms |
37668 KB |
Output is correct |
40 |
Correct |
133 ms |
37552 KB |
Output is correct |
41 |
Correct |
89 ms |
37492 KB |
Output is correct |
42 |
Correct |
161 ms |
37584 KB |
Output is correct |
43 |
Correct |
169 ms |
42144 KB |
Output is correct |
44 |
Correct |
99 ms |
37584 KB |
Output is correct |