제출 #1029928

#제출 시각아이디문제언어결과실행 시간메모리
1029928mychecksedadAwesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
50 ms25680 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 510;
#define pii pair<int,int>
#define pb push_back
struct Edge{
    int x, y, w;
};
 
int n, m, dist[N][N];
char c;
vector<Edge> g[N][N];
bool vis[N][N];
 
 
int main(){
    cin.tie(0); ios::sync_with_stdio(0);
    cin >> n >> m;
    int arr[4][2]={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    for(int i = 0; i <= n+2; i++) for(int j = 0; j <= m+2; j++) {dist[i][j] = 1e9; vis[i][j] = 0;}
    Edge e;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> c;
            int co;
            if(c == 'N'){
                co = 0;
                for(int k = 0; k < 4; k++){
                    e.x = i + arr[(k+co)%4][0];
                    e.y = j + arr[(k+co)%4][1];
                    e.w = k;
                    g[i][j].pb(e);
                }
            }else if(c == 'E'){
                co=1;
                for(int k = 0; k < 4; k++){
                    e.x = i + arr[(k+co)%4][0];
                    e.y = j + arr[(k+co)%4][1];
                    e.w = k;
                    g[i][j].pb(e);
                }
            }else if(c == 'S'){
                co=2;
                for(int k = 0; k < 4; k++){
                    e.x = i + arr[(k+co)%4][0];
                    e.y = j + arr[(k+co)%4][1];
                    e.w = k;
                    g[i][j].pb(e);
                }
            }else if(c == 'W'){
                co=3;
                for(int k = 0; k < 4; k++){
                    e.x = i + arr[(k+co)%4][0];
                    e.y = j + arr[(k+co)%4][1];
                    e.w = k;
                    g[i][j].pb(e);
                }
            }
        }   
    }
    dist[1][1] = 0;
    queue<pii> q[4];
    q[0].push({1, 1});
    int cur = 0;
    while(!q[0].empty() || !q[1].empty() || !q[2].empty() || !q[3].empty()){
        if(q[cur].empty()){
          (cur=cur+1)%=4;
          continue;
        }
        int x = q[cur].front().first, y = q[cur].front().second;
        q[cur].pop();
        if(vis[x][y]) continue;
        vis[x][y] = 1;
        for(Edge u: g[x][y]){
            if(dist[x][y] + u.w < dist[u.x][u.y]){
                dist[u.x][u.y] = dist[x][y] + u.w;
                q[(cur + u.w) % 4].push({u.x, u.y});
            }
        }
    }
    cout << (!vis[n][m] ? -1 : dist[n][m]);
    return 0;
}
#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...