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 P pair<int, pair<int, int>>
#define s second
#define f first
using namespace std;
using ll = long long;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char dz[4] = {'E', 'W', 'S', 'N'};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
char s[n + 1][m + 1];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> s[i][j];
}
}
map<pair<char, char>, int> M;
M[{'N', 'N'}] = 0;
M[{'S', 'S'}] = 0;
M[{'E', 'E'}] = 0;
M[{'W', 'W'}] = 0;
M[{'N', 'E'}] = 1;
M[{'E', 'S'}] = 1;
M[{'S', 'W'}] = 1;
M[{'W', 'N'}] = 1;
M[{'N', 'S'}] = 2;
M[{'E', 'W'}] = 2;
M[{'S', 'N'}] = 2;
M[{'W', 'E'}] = 2;
M[{'N', 'W'}] = 3;
M[{'E', 'N'}] = 3;
M[{'S', 'E'}] = 3;
M[{'W', 'S'}] = 3;
priority_queue<P, vector<P>, greater<P>> pq;
pq.push({0, {1, 1}});
vector<vector<int>> dis(n + 1, vector<int> (m + 1, 1e9));
dis[1][1] = 0;
while(!pq.empty()) {
int z = pq.top().f;
int x = pq.top().s.f;
int y = pq.top().s.s;
pq.pop();
if(z != dis[x][y] || s[x][y] == 'X') continue;
for(int i = 0; i < 4; i++) {
int X = x + dx[i];
int Y = y + dy[i];
int Z = M[{s[x][y], dz[i]}];
if(X > 0 && Y > 0 && X <= n && Y <= m && dis[X][Y] > dis[x][y] + Z) {
dis[X][Y] = dis[x][y] + Z;
pq.push({dis[X][Y], {X, Y}});
}
}
}
cout << (dis[n][m] == 1e9 ? -1 : dis[n][m]) << '\n';
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... |