#include <bits/stdc++.h>
#define int long long
using namespace std;
struct dirs_struct {
const int INF = 1e18;
int N = 0;
int E = 1;
int S = 2;
int W = 3;
int get_dir(char dir) {
if (dir == 'N') {
return N;
} else if (dir == 'E') {
return E;
} else if (dir == 'S') {
return S;
} else if (dir == 'W') {
return W;
} else {
return -1;
}
}
int get_dir(int dx, int dy) {
if (dx == 0 && dy == -1) {
return N;
} else if (dx == 1 && dy == 0) {
return E;
} else if (dx == 0 && dy == 1) {
return S;
} else if (dx == -1 && dy == 0) {
return W;
} else {
return -1;
}
}
int get_cost(char direction1, int dx, int dy) {
int dir1 = get_dir(direction1);
int dir2 = get_dir(dx, dy);
if (dir1 == -1 || dir2 == -1) {
return INF;
} else {
return ((dir2 - dir1) + 4) % 4;
}
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
const int INF = 1e18;
dirs_struct dirs;
int m, n;
cin >> m >> n;
vector<vector<char>> grid(m+2, vector<char>(n+2, 'X'));
for (int y=0; y<m; y++) {
for (int x=0; x<n; x++) {
cin >> grid[y+1][x+1];
}
}
vector<vector<int>> cost(m+2, vector<int>(n+2, INF));
priority_queue<tuple<int, int, int>,
vector<tuple<int, int, int>>,
greater<tuple<int, int, int>>> pq;
cost[1][1] = 0;
pq.emplace(0, 0, 0);
while (!pq.empty()) {
auto [c, x, y] = pq.top();
pq.pop();
//cerr << c << " " << x << " " << y << "\n";
if (grid[y+1][x+1] == 'X') { continue; }
array<pair<int, int>, 4> offsets {make_pair(1, 0), make_pair(-1, 0),
make_pair(0, 1), make_pair(0, -1)};
for (auto [dx, dy] : offsets) {
int new_cost = c + dirs.get_cost(grid[y+1][x+1], dx, dy);
if (cost[y+dy+1][x+dx+1] > new_cost) {
cost[y+dy+1][x+dx+1] = new_cost;
pq.emplace(new_cost, x+dx, y+dy);
}
}
}
cout << cost[m][n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |