이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAX = 505, INF = 2e9;
typedef pair <int, int> pii;
int n, m, mat[MAX][MAX], sol[MAX][MAX];
set <pair <int, pii>> dijk;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++){
string row;
cin >> row;
for(int j = 0; j < m; j++){
if(row[j] == 'N') mat[i][j] = 0;
else if(row[j] == 'E') mat[i][j] = 1;
else if(row[j] == 'S') mat[i][j] = 2;
else if(row[j] == 'W') mat[i][j] = 3;
else mat[i][j] = -1;
if(i > 0 || j > 0){
sol[i][j] = INF;
dijk.insert({INF, {i, j}});
}
}
}
dijk.insert({0, {0, 0}});
while(!dijk.empty()){
auto now = *dijk.begin();
dijk.erase(now);
int x = now.s.f, y = now.s.s;
if(mat[x][y] == -1) continue;
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m){
int new_val = sol[x][y];
if(i >= mat[x][y]) new_val += (i - mat[x][y]);
else new_val += (i + 4 - mat[x][y]);
if(new_val < sol[nx][ny]){
dijk.erase({sol[nx][ny], {nx, ny}});
sol[nx][ny] = new_val;
dijk.insert({sol[nx][ny], {nx, ny}});
}
}
}
/*cout << x << ' ' << y << '\n';
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) cout << sol[i][j] << ' ';
cout << '\n';
}
cout << '\n';*/
}
if(sol[n - 1][m - 1] >= INF) cout << -1;
else cout << sol[n - 1][m - 1];
return 0;
}
# | 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... |