#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,i,e,f,g,n,m,k,l,A[500005];
string s[505];
vector < pair < long long , long long > > v[500005];
priority_queue< pair < long long , long long > > pq;
int main() {
cin>>n>>m;
for(long long i=1;i<=n;i++) {
cin>>s[i];
s[i]='#'+s[i];
// cout<<s[i]<<endl;
}
//cout<<s[2][1]<<endl;
for(long long i=1;i<=n;i++) {
for(long long j=1;j<=m;j++) {
if(s[i][j]=='X') continue;
a=0;
if(i-1>0) {
if(s[i][j]=='N') a=0;
else if(s[i][j]=='E') a=3;
else if(s[i][j]=='W') a=1;
else a=2;
v[(i-1)*m+j].push_back({(i-2)*m+j,a});
}
a=0;
if(i+1<=n) {
if(s[i][j]=='S') a=0;
else if(s[i][j]=='W') a=3;
else if(s[i][j]=='E') a=1;
else a=2;
v[(i-1)*m+j].push_back({i*m+j,a});
}
a=0;
if(j-1>0) {
if(s[i][j]=='W') a=0;
else if(s[i][j]=='N') a=3;
else if(s[i][j]=='S') a=1;
else if(s[i][j]=='E') a=2;
v[(i-1)*m+j].push_back({(i-1)*m+j-1,a});
}
a=0;
if(j+1<=m) {
if(s[i][j]=='E') a=0;
else if(s[i][j]=='S') a=3;
else if(s[i][j]=='N') a=1;
else if(s[i][j]=='W') a=2;
//if((i-1)*m+j==4) cout<<a<<" "<<s[i][j]<<" "<<i<<" "<<j<<" "<<s[i]<<endl;
v[(i-1)*m+j].push_back({(i-1)*m+j+1,a});
}
}
}
for(long long i=1;i<=n*m;i++)
A[i]=100000000001ll;
/*for(long long i=0;i<v[4].size();i++)
cout<<v[4][i].first<<" "<<v[4][i].second<<endl;
cout<<endl;*/
A[1]=0;
pq.push({0,1});
while (pq.size()>0) {
a=-(pq.top().first);
b=pq.top().second;
pq.pop();
for(long long i=0;i<v[b].size();i++) {
c=v[b][i].first; d=v[b][i].second;
if(A[c]>A[b]+d) { A[c]=A[b]+d; pq.push({-(A[c]), c}); }
}
}
/*for(long long i=1;i<=n*m;i++) {
cout<<A[i]<<" ";
if(i%m==0) cout<<endl;
}*/
if(A[n*m]==100000000001) cout<<-1;
else cout<<A[n*m];
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:65:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(long long i=0;i<v[b].size();i++) {
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12032 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12032 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
8 ms |
12032 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12032 KB |
Output is correct |
19 |
Correct |
8 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
7 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12192 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12064 KB |
Output is correct |
8 |
Correct |
8 ms |
12032 KB |
Output is correct |
9 |
Correct |
8 ms |
12032 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
9 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12160 KB |
Output is correct |
5 |
Correct |
8 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12032 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
8 ms |
12032 KB |
Output is correct |
13 |
Correct |
8 ms |
12160 KB |
Output is correct |
14 |
Correct |
8 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
8 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12032 KB |
Output is correct |
19 |
Correct |
8 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12032 KB |
Output is correct |
22 |
Correct |
7 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12032 KB |
Output is correct |
24 |
Correct |
8 ms |
12032 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12032 KB |
Output is correct |
27 |
Correct |
8 ms |
12160 KB |
Output is correct |
28 |
Correct |
8 ms |
12160 KB |
Output is correct |
29 |
Correct |
8 ms |
12192 KB |
Output is correct |
30 |
Correct |
8 ms |
12160 KB |
Output is correct |
31 |
Correct |
8 ms |
12064 KB |
Output is correct |
32 |
Correct |
8 ms |
12032 KB |
Output is correct |
33 |
Correct |
8 ms |
12032 KB |
Output is correct |
34 |
Correct |
8 ms |
12032 KB |
Output is correct |
35 |
Correct |
16 ms |
13696 KB |
Output is correct |
36 |
Correct |
8 ms |
12288 KB |
Output is correct |
37 |
Correct |
18 ms |
14080 KB |
Output is correct |
38 |
Correct |
17 ms |
15104 KB |
Output is correct |
39 |
Correct |
110 ms |
30328 KB |
Output is correct |
40 |
Correct |
109 ms |
30200 KB |
Output is correct |
41 |
Correct |
65 ms |
30116 KB |
Output is correct |
42 |
Correct |
110 ms |
30200 KB |
Output is correct |
43 |
Correct |
152 ms |
38248 KB |
Output is correct |
44 |
Correct |
62 ms |
30200 KB |
Output is correct |