結果

問題 No.424 立体迷路
ユーザー xuzijian629xuzijian629
提出日時 2018-10-30 00:27:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,701 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 209,216 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 17:05:37
合計ジャッジ時間 3,238 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

int main() {
    int h, w;
    cin >> h >> w;

    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;

    vvi b(h, vi(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            char c;
            cin >> c;
            b[i][j] = c - '0';
        }
    }

    vvi dist(h, vi(w, 1e9));
    dist[sx][sy] = 0;
    using ii = pair<int, int>;
    queue<ii> que;
    que.push(ii(sx, sy));
    auto ok = [&](int x, int y) {
        return 0 <= x && x < h && 0 <= y && y < w;
    };

    int dx[] = {-1, 1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    while (que.size()) {
        ii t = que.front();
        que.pop();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 2; j++) {
                int x = t.first + dx[i] * j;
                int y = t.second + dy[i] * j;
                if (ok(x, y)) {
                    if (j == 2) {
                        if (b[t.first][t.second] != b[x][y]) continue;
                        if (!(b[t.first + dx[i]][t.second + dy[i]] < b[t.first][t.second])) continue;
                    } else {
                        if (abs(b[t.first][t.second] - b[x][y]) > 1) continue;
                    }
                    if (dist[t.first][t.second] + 1 < dist[x][y]) {
                        dist[x][y] = dist[t.first][t.second] + 1;
                        que.push(ii(x, y));
                    }
                }
            }
        }
    }

    if (dist[gx][gy] != 1e9) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}   
0