結果

問題 No.424 立体迷路
ユーザー はむこはむこ
提出日時 2016-09-19 08:00:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,000 bytes
コンパイル時間 3,074 ms
コンパイル使用メモリ 154,184 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:40:25
合計ジャッジ時間 2,704 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
using ll = long long; using vll = vector<ll>; using vvll = vector<vll>;

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) { }
    // x, yをマージ
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    // x, yが同じ集合なら1
    bool find(int x, int y) {
        return root(x) == root(y);
    }
    // xの根を探す。同じ集合なら同じ根が帰る
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
};

#define get(i, j) (b[i+buff/2][j+buff/2])
#define encode(i, j) (i*w+j)
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    ll h, w; cin >> h >> w;
    ll sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;

    const int buff = 10;
    vvll b(h+buff, vll(w+buff, -5));
    rep(i, h) {
        string s; cin >> s;
        rep(j, s.length()) {
            get(i, j) = s[j] - '0';
        }
    }

    vll di = {1, -1, 0, 0};
    vll dj = {0, 0, 1, -1};
    UnionFind uf(h*w);
    rep(i, h) {
        rep(j, w) {
            rep(d, 4) {
                ll nexti = i + di[d];
                ll nextj = j + dj[d];
                if (abs(get(i, j) - get(nexti, nextj)) <= 1)
                    uf.unite(encode(i, j), encode(nexti, nextj));
            }
            rep(d, 4) {
                ll nexti = i + di[d] * 2;
                ll nextj = j + dj[d] * 2;
                if (get(i, j) == get(nexti, nextj) && get(i, j) > get(i + di[d], j + dj[d]))
                    uf.unite(encode(i, j), encode(nexti, nextj));
            }
        }
    }

    if (uf.find(encode(sx, sy), encode(gx, gy))) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}
0