結果

問題 No.424 立体迷路
ユーザー rogi52rogi52
提出日時 2022-08-13 15:33:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,525 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 210,372 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 23:45:57
合計ジャッジ時間 3,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

class union_find {
  public:
    union_find(int n) : data(n, -1) {}
    int unite(int x, int y) {
        x = root(x), y = root(y);
        if(x != y) {
            if(size(x) < size(y)) swap(x, y);
            data[x] += data[y];
            return data[y] = x;
        }
        return -1;
    }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    bool same(int x, int y) { return root(x) == root(y); }

  private:
    vector<int> data;
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int h,w; cin >> h >> w;
    int sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--;
    auto hash = [&](int x, int y){ return x * w + y; };
    vector<string> b(h);
    rep(i,h) cin >> b[i];

    union_find uf(h * w);
    auto ok = [&](int x, int y){ return 0 <= x && x < h && 0 <= y && y < w; };
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    rep(x,h)rep(y,w)rep(d,4) {
        int nx = x + dx[d], ny = y + dy[d];
        if(ok(nx, ny)) {
            if(abs(b[x][y] - b[nx][ny]) <= 1) uf.unite(hash(x, y), hash(nx, ny));
        }
        int mx = x + dx[d] * 2, my = y + dy[d] * 2;
        if(ok(mx, my)) {
            if(b[x][y] == b[mx][my] && b[x][y] > b[nx][ny]) uf.unite(hash(x, y), hash(mx, my));
        }
    }

    cout << (uf.same(hash(sx, sy), hash(gx, gy)) ? "YES" : "NO") << endl;
}
0