Submission #976844

#TimeUsernameProblemLanguageResultExecution timeMemory
976844vjudge1Awesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
110 ms10436 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...