This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author: erray
#include<bits/stdc++.h>
using namespace std;
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<int> h(256);
h['X'] = 4;
h['N'] = 0;
h['E'] = 1;
h['S'] = 2;
h['W'] = 3;
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; ++i) {
cin >> grid[i];
for (auto& c : grid[i]) {
c = h[c];
}
}
vector<vector<int>> cost(n, vector<int>(m, -1));
struct pqNode {
int x, y, c;
bool operator< (const pqNode& ot) const {
return c > ot.c;
}
};
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
priority_queue<pqNode> pq;
pq.push(pqNode{0, 0, 0});
while (!pq.empty()) {
int x = pq.top().x, y = pq.top().y, c = pq.top().c;
pq.pop();
if (cost[x][y] != -1) continue;
cost[x][y] = c;
if (grid[x][y] == 4) {
continue;
}
for (int d = 0; d < 4; ++d) {
int nw = (int) grid[x][y] + d;
nw %= 4;
int nx = x + dx[nw], ny = y + dy[nw];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
pq.push(pqNode{nx, ny, c + d});
}
}
cout << cost[n - 1][m - 1] << '\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... |