結果

問題 No.424 立体迷路
ユーザー tubo28tubo28
提出日時 2016-09-22 22:58:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,339 bytes
コンパイル時間 1,087 ms
コンパイル使用メモリ 117,044 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:49:29
合計ジャッジ時間 2,130 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
using ll = long long;

int h, w;
char g[55][55];
int si, sj, gi, gj;

struct UnionFind {
    vector<int> par;
    int cnt;
    UnionFind(int size_) : par(size_, -1), cnt(size_) {}
    void unite(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (par[y] < par[x]) swap(x, y);
            par[x] += par[y]; par[y] = x; cnt--;
        }
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); }
    int size(int x) { return -par[find(x)]; }
    int size() { return cnt; }
};

bool check(int i, int j, int ni, int nj) {
    int a = g[i][j] - '0';
    int b = g[ni][nj] - '0';
    return abs(a - b) <= 1;
}

bool check2(int i, int j, int ni, int nj) {
    int a = g[i][j] - '0';
    int b = g[ni][nj] - '0';
    int c = g[(i + ni) / 2][(j + nj) / 2] - '0';
    return a == b && a > c;
}

int enc(int i, int j) {
    return i*w + j;
}

int main() {
    while (cin >> h >> w) {
        UnionFind uf(h*w);
        cin >> si >> sj >> gi >> gj;
        --si; --sj; --gi; --gj;
        for (int i = 0; i < h; i++) cin >> g[i];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (i >= 1) if (check(i, j, i - 1, j)) uf.unite(enc(i, j), enc(i - 1, j));
                if (j >= 1) if (check(i, j, i, j - 1)) uf.unite(enc(i, j), enc(i, j - 1));
                if (i >= 2) if (check2(i, j, i - 2, j)) uf.unite(enc(i, j), enc(i - 2, j));
                if (j >= 2) if (check2(i, j, i, j - 2)) uf.unite(enc(i, j), enc(i, j - 2));
            }
        }
        
        puts(uf.same(enc(si, sj), enc(gi, gj)) ? "YES" : "NO");
    }
}
0