#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= 0x3f;
#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];
}
}
dis[1][1][arah(1,1)] = 0;
pq.push({0,1,1,arah(1,1)});
memset(dis, INF, sizeof(dis));
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
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
1 ms |
5212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
5208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |