제출 #976869

#제출 시각아이디문제언어결과실행 시간메모리
976869vjudge1Awesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
112 ms15036 KiB
#include<bits/stdc++.h>
using namespace std;
#define debug cout << "here\n";
#define koor pair<int, pair<int, int>>
#define fi first
#define se second
int n,m;

bool ketemu;
char taman[503][503];
bool explored[503][503];
int dx[8] = {-1,0,1,0,-1,0,1,0},
    dy[8] = {0,1,0,-1,0,1,0,-1};

void djikstra(){
    priority_queue<koor, vector<koor>, greater<>> pq;
    pq.push({0,{1,1}});

    while(!pq.empty()){
        int putar = pq.top().fi,
            x     = pq.top().se.fi,
            y     = pq.top().se.se;
        pq.pop();

        //cout << x << ", " << y << "  ->" << putar << '\n';


        if(x==n && y==m){
            cout << putar;
            ketemu = true;
            break;
        }

        if(explored[x][y] || taman[x][y] == 'X') continue;
        else explored[x][y] = true;
        
        char arah = taman[x][y];
        if(arah == 'N'){
            for(int i = 0; i < 4; i++){
                if(x+dx[i] <= n && y+dy[i] <= m && x+dx[i] >= 1 && y+dy[i] >= 1)
                    pq.push({putar+i,  { x+dx[i], y+dy[i] } }  );
            }
        }else if(arah == 'E'){
            for(int i = 1; i < 5; i++){
                if(x+dx[i] <= n && y+dy[i] <= m && x+dx[i] >= 1 && y+dy[i] >= 1)
                    pq.push({putar+i-1,  { x+dx[i], y+dy[i] } }  );
            }
        }else if(arah == 'S'){
            for(int i = 2; i < 6; i++){
                if(x+dx[i] <= n && y+dy[i] <= m && x+dx[i] >= 1 && y+dy[i] >= 1)
                    pq.push({putar+i-2,  { x+dx[i], y+dy[i] } }  );
            }
        }else {
            for(int i = 3; i < 7; i++){
                if(x+dx[i] <= n && y+dy[i] <= m && x+dx[i] >= 1 && y+dy[i] >= 1)
                    pq.push({putar+i-3,  { x+dx[i], y+dy[i] } }  );
            }
        }
    }
}

int main (){
    cin >> n >> m;


    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> taman[i][j];
        }
    }

    djikstra();
    if(!ketemu) cout << -1;
    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...