#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define LE(vec) vec[vec.size()-1]
#define TSTS int t; cin >> t; while(t--)solve()
const int INF = 1e9;
const long long LINF = 1e18;
const long double PI = asin(1)*2;
const int MOD = 1e9+7;
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
typedef long double ld;
void setIO()
{
FAST_IO;
}
const int N=510;
int m, n, d[N][N], g[N][N];
bool v[N][N];
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> q;
/*
-1-X
0-N
1-E
2-S
3-W
*/
int c(char x)
{
if(x=='X') return -1;
if(x=='N') return 0;
if(x=='E') return 1;
if(x=='S') return 2;
if(x=='W') return 3;
}
pii re(int p, int x, int y)
{
if(p==0) return MP(x, y-1);
if(p==1) return MP(x+1, y);
if(p==2) return MP(x, y+1);
if(p==3) return MP(x-1, y);
}
int main()
{
setIO();
cin >> m >> n;
FOR(i, 0, m)
{
FOR(j, 0, n)
{
char x;
cin >> x;
g[j][i]=c(x);
}
}
FOR(i, 0, m) FOR(j, 0, n) d[j][i]=INF;
v[n-1][m-1]=true;
d[0][0]=0;
q.push(MP(0, MP(0, 0)));
while(!q.empty())
{
int k=q.top().F, x=q.top().S.F, y=q.top().S.S;
if(!v[x][y])
{
v[x][y]=true;
FOR(i, 0, 4)
{
int nx=re((g[x][y]+i)%4, x, y).F, ny=re((g[x][y]+i)%4, x, y).S;
if(nx<0 || nx>=n || ny<0 || ny>=m || (g[nx][ny]==-1 && (nx!=n-1 || ny!=m-1))) continue;
d[nx][ny]=min(d[nx][ny], k+i);
q.push(MP(d[nx][ny], MP(nx, ny)));
}
}
q.pop();
}
cout << (d[n-1][m-1]==INF?-1:d[n-1][m-1]);
}
Compilation message
adventure.cpp: In function 'int c(char)':
adventure.cpp:50:1: warning: control reaches end of non-void function [-Wreturn-type]
50 | }
| ^
adventure.cpp: In function 'pii re(int, int, int)':
adventure.cpp:58:1: warning: control reaches end of non-void function [-Wreturn-type]
58 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
2124 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
2124 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
2124 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
2124 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1868 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
2 ms |
2124 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1612 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
2124 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
2124 KB |
Output is correct |
10 |
Correct |
1 ms |
716 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
1740 KB |
Output is correct |
13 |
Correct |
1 ms |
2380 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1868 KB |
Output is correct |
17 |
Correct |
1 ms |
1996 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
2380 KB |
Output is correct |
21 |
Correct |
0 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |