結果

問題 No.424 立体迷路
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-02-04 03:11:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,478 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 8,216 KB
最終ジャッジ日時 2023-09-12 22:23:54
合計ジャッジ時間 1,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,984 KB
testcase_01 AC 17 ms
8,116 KB
testcase_02 AC 17 ms
8,100 KB
testcase_03 AC 17 ms
8,120 KB
testcase_04 AC 18 ms
8,136 KB
testcase_05 AC 17 ms
8,032 KB
testcase_06 AC 17 ms
8,084 KB
testcase_07 AC 17 ms
8,040 KB
testcase_08 AC 17 ms
8,192 KB
testcase_09 AC 17 ms
8,104 KB
testcase_10 AC 17 ms
8,024 KB
testcase_11 AC 17 ms
8,140 KB
testcase_12 AC 17 ms
8,060 KB
testcase_13 AC 17 ms
8,032 KB
testcase_14 AC 17 ms
8,080 KB
testcase_15 AC 17 ms
8,216 KB
testcase_16 AC 17 ms
8,072 KB
testcase_17 AC 22 ms
8,020 KB
testcase_18 AC 21 ms
8,164 KB
testcase_19 AC 21 ms
8,068 KB
testcase_20 AC 22 ms
8,036 KB
testcase_21 AC 17 ms
8,100 KB
testcase_22 AC 17 ms
8,208 KB
testcase_23 AC 17 ms
8,104 KB
testcase_24 AC 24 ms
8,028 KB
testcase_25 AC 25 ms
8,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.count = n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return self.rank[x]

    def count(self):
        return self.count

h, w = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
s = [list(map(int, list(input()))) for i in range(h)]
UF = UnionFind(h * w)
for i in range(h):
    for j in range(w - 1):
        if abs(s[i][j] - s[i][j + 1]) <= 1:
            UF.unite(i * w + j, i * w + j + 1)
        if j != w - 2 and s[i][j] == s[i][j + 2] > s[i][j + 1]:
            UF.unite(i * w + j, i * w + j + 2)
for i in range(h - 1):
    for j in range(w):
        if abs(s[i][j] - s[i + 1][j]) <= 1:
            UF.unite(i * w + j, (i + 1) * w + j)
        if i != h - 2 and s[i][j] == s[i + 2][j] > s[i + 1][j]:
            UF.unite(i * w + j, (i + 2) * w + j)
print("YES" if UF.same((sx - 1) * w + (sy - 1), (gx - 1) * w + (gy - 1)) else "NO")
0