This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define umin(x, y) x = min(x, y)
#define sq(x) ((x)*(x))
using namespace std;
const int N = 509;
const ll INF = 2e9 + 7;
int n, m;
char g[N][N], gg[N][N];
int clc(char f, char t) {
if (f == t) return 0;
if (f == 'W') {
if (t == 'N') return 1;
if (t == 'E') return 2;
if (t == 'S') return 3;
}
if (f == 'S') {
if (t == 'W') return 1;
if (t == 'N') return 2;
if (t == 'E') return 3;
}
if (f == 'E') {
if (t == 'S') return 1;
if (t == 'W') return 2;
if (t == 'N') return 3;
}
if (t == 'E') return 1;
if (t == 'S') return 2;
if (t == 'W') return 3;
}
bool vis[N][N];
int dist[N][N];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 0; i <= n+1; i++) {
g[0][i] = 'X';
g[i][m+1] = 'X';
}
for (int i = 0; i <= m + 1; i++) {
g[i][0] = 'X';
g[n+1][i] = 'X';
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> g[i][j];
dist[i][j] = INF;
}
}
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
dist[1][1] = 0;
pq.push({ 0, {1, 1} });
while (pq.size())
{
auto [cost, pnt] = pq.top();
auto [x, y] = pnt;
pq.pop();
if (vis[x][y] || g[x][y] == 'X') continue;
vis[x][y] = 1;
if (dist[x - 1][y] > cost + clc(g[x][y], 'N')) {
dist[x - 1][y] = cost + clc(g[x][y], 'N');
pq.push({ dist[x + 1][y], {x-1, y} });
}
if (dist[x + 1][y] > cost + clc(g[x][y], 'S')) {
dist[x + 1][y] = cost + clc(g[x][y], 'S');
pq.push({ dist[x + 1][y], {x+1, y} });
}
if (dist[x][y + 1] > cost + clc(g[x][y], 'E')) {
dist[x][y + 1] = cost + clc(g[x][y], 'E');
pq.push({ dist[x][y + 1], {x, y+1} });
}
if (dist[x][y - 1] > cost + clc(g[x][y], 'W')) {
dist[x][y - 1] = cost + clc(g[x][y], 'W');
pq.push({ dist[x][y - 1], {x, y-1} });
}
}
cout << (dist[n][m] == INF ? -1 : dist[n][m]);
}
Compilation message (stderr)
adventure.cpp: In function 'int clc(char, char)':
adventure.cpp:34:1: warning: control reaches end of non-void function [-Wreturn-type]
34 | }
| ^
# | 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... |