제출 #439928

#제출 시각아이디문제언어결과실행 시간메모리
439928elazarkorenAwesome Arrowland Adventure (eJOI19_adventure)C++17
0 / 100
1 ms216 KiB
#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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...