#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;
bool vis[501]; ll dist[501];
vector<pii> adj[501];
priority_queue<pii,vector<pii>, greater<pii>> q;
int pirmine[501];
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:79:10: warning: unused variable 'd' [-Wunused-variable]
79 | int d = pirmine[n*m-1]; //cout<<d<<endl;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |