이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int m, n;
vector<vector<char>> grid;
signed main(){
cin >> m >> n;
grid.resize(m, vector<char>(n));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cin >> grid[i][j];
}
}
priority_queue<pair<int, pair<int, int>>> pq;
pq.push({0, {0, 0}});
vector<vector<int>> visited(m, vector<int>(n, -1));
while(!pq.empty()){
int a = -pq.top().first;
int x = pq.top().second.first;
int y = pq.top().second.second;
pq.pop();
if(x>n-1 || x<0 || y>m-1 || y<0)continue;
if(visited[y][x]!=-1) continue;
visited[y][x]=a;
if(grid[y][x]=='X')continue;
if(grid[y][x]=='E'){
pq.push({-a, {x+1, y}});
pq.push({-a-1, {x, y+1}});
pq.push({-a-2, {x-1, y}});
pq.push({-a-3, {x, y-1}});
}
else if(grid[y][x]=='S'){
pq.push({-a-3, {x+1, y}});
pq.push({-a, {x, y+1}});
pq.push({-a-1, {x-1, y}});
pq.push({-a-2, {x, y-1}});
}
else if(grid[y][x]=='W'){
pq.push({-a-2, {x+1, y}});
pq.push({-a-3, {x, y+1}});
pq.push({-a, {x-1, y}});
pq.push({-a-1, {x, y-1}});
}
else if(grid[y][x]=='N'){
pq.push({-a-1, {x+1, y}});
pq.push({-a-2, {x, y+1}});
pq.push({-a-3, {x-1, y}});
pq.push({-a, {x, y-1}});
}
}
cout << visited[m-1][n-1] << "\n";
/*for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << grid[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << visited[i][j] << " ";
}
cout << "\n";
}*/
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |