#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pll pair<ll,ll>
const ll MOD=1e9+7;
const ll INF= 1e9;
#define ll int
//KALAU TAKUT RTE
bool cmp (pair<ll,ll> x, pair<ll,ll>y){
return x.second < y.second;
}
ll expo(ll x, ll y){
if (y==0) return 1;
ll ans = expo((x*x)%MOD, y/2);
if (x%2) return (ans * x)%MOD;
return ans%MOD;
}
ll dx[]={0, 1, 0, -1};
ll dy[]={-1, 0, 1, 0};
char grid[505][505];
ll dis[505][505][5];
typedef tuple<ll,ll,ll,ll> state;
priority_queue<state, vector<state>, greater<state>> pq;
ll arah(ll x, ll y){
if (grid[x][y] == 'N') return 0;
if (grid[x][y] == 'E') return 1;
if (grid[x][y] == 'S') return 2;
if (grid[x][y] == 'W') return 3;
return 4;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll n, m; cin>>n>>m;
for (int i=1; i<=n;i++){
for (int j=1; j<=m;j++){
cin>>grid[i][j];
for (int k=0; k<5;k++) dis[i][j][k]= INF;
}
}
dis[1][1][arah(1,1)] = 0;
pq.push({0,1,1,arah(1,1)});
while(!pq.empty()){
auto [cost,y,x,d] = pq.top(); pq.pop();
// cout<< cost<<" "<<y<<" "<<x<<" "<<d<<endl;
if (d==4) continue;
int nx = x+dx[d], ny=y+dy[d];
if (nx > 0 && nx<=m && ny>0 && ny<=n){
int nd = arah(ny,nx);
if (dis[ny][nx][nd] > cost){
dis[ny][nx][nd] = cost;
pq.push({cost, ny,nx,nd});
}
}
int nd = (d+1)%4;
if (dis[y][x][nd] > cost+1){
dis[y][x][nd]=cost+1;
pq.push({cost+1, y, x, nd});
}
}
if (dis[n][m][4] == INF) cout<<-1<<endl;
else cout<<dis[n][m][4]<<endl;
}
Compilation message
adventure.cpp:11: warning: "ll" redefined
11 | #define ll int
|
adventure.cpp:4: note: this is the location of the previous definition
4 | #define ll long long
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
464 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
9 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
12 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
105 ms |
5972 KB |
Output is correct |
40 |
Correct |
105 ms |
6000 KB |
Output is correct |
41 |
Correct |
4 ms |
5724 KB |
Output is correct |
42 |
Correct |
105 ms |
5948 KB |
Output is correct |
43 |
Correct |
110 ms |
10436 KB |
Output is correct |
44 |
Correct |
4 ms |
5720 KB |
Output is correct |