#include <iostream>
#include <vector>
#include <algorithm>
#define chkmin(a, b) a = min(a, b)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int infinity = 1e9;
int ans = infinity;
int dir_x[] = {-1, 0, 1, 0};
int dir_y[] = {0, 1, 0, -1};
int n, m;
void Dfs(vvi &graph, int x, int y, vvb &visited, int count) {
if (x < 0 || x >= n || y < 0 || y >= m) return;
if (visited[x][y]) return;
if (x == n - 1 && y == m - 1) {
chkmin(ans, count);
return;
}
if (graph[x][y] == 4) return;
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
Dfs(graph, x + dir_x[graph[x][y]], y + dir_y[graph[x][y]], visited, count + i);
graph[x][y]++;
graph[x][y] %= 4;
}
visited[x][y] = false;
}
int main() {
cin >> n >> m;
vvi graph(n, vi(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c == 'X') {
graph[i][j] = 4;
} else if (c == 'S') {
graph[i][j] = 2;
} else if (c == 'W') {
graph[i][j] = 3;
} else if (c == 'E') {
graph[i][j] = 1;
}
}
}
vvb visited(n, vb(m));
Dfs(graph, 0, 0, visited, 0);
cout << (ans == infinity ? ans : -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 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 |
Incorrect |
0 ms |
216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |