#pragma gcc diagnostic "-std=c++1z"
#pragma gcc optimize("Ofast");
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define f first
#define s second
#define MOD 1000000007
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
using namespace std;
const int N=505;
char arr[N][N];
int n,m,T,k,dist[N*N],fix[N*N];
vector<pii> g[N*N];
int val(int a, int b) {
return (a-1)*m+b;
}
main () {
ios :: sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=1; i<=n; i++) {
string s; cin>>s;
for (int j=1; j<=m; j++) {
dist[val(i,j)]=1e18;
if (s[j-1]=='S') {
if (i<n) g[val(i,j)].pb({val(i+1,j),0});
if (j<m) g[val(i,j)].pb({val(i,j+1),3});
if (i>1) g[val(i,j)].pb({val(i-1,j),2});
if (j>1) g[val(i,j)].pb({val(i,j-1),1});
}
if (s[j-1]=='E') {
if (i<n) g[val(i,j)].pb({val(i+1,j),1});
if (j<m) g[val(i,j)].pb({val(i,j+1),0});
if (i>1) g[val(i,j)].pb({val(i-1,j),3});
if (j>1) g[val(i,j)].pb({val(i,j-1),2});
}
if (s[j-1]=='W') {
if (i<n) g[val(i,j)].pb({val(i+1,j),3});
if (j<m) g[val(i,j)].pb({val(i,j+1),2});
if (i>1) g[val(i,j)].pb({val(i-1,j),1});
if (j>1) g[val(i,j)].pb({val(i,j-1),0});
}
if (s[j-1]=='N') {
if (i<n) g[val(i,j)].pb({val(i+1,j),2});
if (j<m) g[val(i,j)].pb({val(i,j+1),1});
if (i>1) g[val(i,j)].pb({val(i-1,j),0});
if (j>1) g[val(i,j)].pb({val(i,j-1),3});
}
}
}
dist[1]=0;
priority_queue<pii> q; q.push({0,1});
while (!q.empty()) {
int v=q.top().s; q.pop();
if (fix[v]) continue;
fix[v]=1;
for (auto x:g[v]) {
int u=x.f,w=x.s;
if (dist[u]>dist[v]+w) {
dist[u]=dist[v]+w;
q.push({-dist[u],u});
}
}
}
// for (int i=1; i<=n; i++) {
// for (int j=1; j<=m; j++) {
// cout<<i<<" "<<j<<" "<<val(i,j)<<" "<<dist[val(i,j)]<<endl;
// }
// }
if (dist[n*m]==(int)1e18) cout<<-1<<endl;
else cout<<dist[n*m]<<endl;
}
Compilation message
adventure.cpp:1: warning: ignoring '#pragma gcc diagnostic' [-Wunknown-pragmas]
1 | #pragma gcc diagnostic "-std=c++1z"
|
adventure.cpp:2: warning: ignoring '#pragma gcc optimize' [-Wunknown-pragmas]
2 | #pragma gcc optimize("Ofast");
|
adventure.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 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 |
2 ms |
8792 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 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 |
8792 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8804 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 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 |
2 ms |
8792 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8804 KB |
Output is correct |
32 |
Correct |
2 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
35 |
Correct |
6 ms |
10296 KB |
Output is correct |
36 |
Correct |
2 ms |
8792 KB |
Output is correct |
37 |
Correct |
7 ms |
10332 KB |
Output is correct |
38 |
Correct |
5 ms |
11356 KB |
Output is correct |
39 |
Correct |
61 ms |
26108 KB |
Output is correct |
40 |
Correct |
66 ms |
25944 KB |
Output is correct |
41 |
Correct |
26 ms |
25936 KB |
Output is correct |
42 |
Correct |
68 ms |
26196 KB |
Output is correct |
43 |
Correct |
75 ms |
34188 KB |
Output is correct |
44 |
Correct |
25 ms |
26056 KB |
Output is correct |