結果

問題 No.424 立体迷路
ユーザー rpy3cpprpy3cpp
提出日時 2016-09-22 22:44:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,672 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 8,616 KB
最終ジャッジ日時 2023-09-18 16:45:10
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,492 KB
testcase_01 AC 17 ms
8,328 KB
testcase_02 AC 17 ms
8,460 KB
testcase_03 AC 17 ms
8,476 KB
testcase_04 AC 17 ms
8,040 KB
testcase_05 AC 17 ms
8,452 KB
testcase_06 AC 17 ms
8,456 KB
testcase_07 AC 17 ms
8,516 KB
testcase_08 AC 17 ms
8,320 KB
testcase_09 AC 18 ms
8,444 KB
testcase_10 AC 17 ms
8,512 KB
testcase_11 AC 16 ms
8,456 KB
testcase_12 AC 17 ms
8,368 KB
testcase_13 AC 17 ms
8,120 KB
testcase_14 AC 17 ms
8,068 KB
testcase_15 AC 17 ms
8,092 KB
testcase_16 AC 17 ms
8,040 KB
testcase_17 AC 19 ms
8,616 KB
testcase_18 AC 19 ms
8,460 KB
testcase_19 AC 20 ms
8,608 KB
testcase_20 AC 20 ms
8,416 KB
testcase_21 AC 17 ms
8,520 KB
testcase_22 AC 17 ms
8,152 KB
testcase_23 AC 16 ms
8,376 KB
testcase_24 AC 22 ms
8,420 KB
testcase_25 AC 21 ms
8,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class DisjointSet(object):
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
        self.num = n  # number of disjoint sets

    def union(self, x, y):
        self._link(self.find_set(x), self.find_set(y))

    def _link(self, x, y):
        if x == y:
            return
        self.num -= 1
        if self.rank[x] > self.rank[y]:
            self.parent[y] = x
        else:
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def find_set(self, x):
        xp = self.parent[x]
        if xp != x:
            self.parent[x] = self.find_set(xp)
        return self.parent[x]


def read_data():
    h, w = map(int, input().split())
    sx, sy, gx, gy = map(int, input().split())
    bs = []
    for _ in range(h):
        b = input()
        bs.append(list(map(int, b)))
    return h, w, sx - 1, sy - 1, gx - 1, gy - 1, bs

def solve(h, w, sx, sy, gx, gy, bs):
    hw = h * w
    ds = DisjointSet(hw)
    for x in range(h):
        for y in range(w):
            pos = x * w + y
            b = bs[x][y]
            if x > 0 and abs(bs[x-1][y] - b) <= 1:
                ds.union(pos, pos - w)
            if y > 0 and abs(bs[x][y-1] - b) <= 1:
                ds.union(pos, pos - 1)
            if x > 1 and bs[x-2][y] == b and bs[x-1][y] < b:
                    ds.union(pos, pos - w * 2)
            if y > 1 and bs[x][y-2] == b and bs[x][y-1] < b:
                    ds.union(pos, pos - 2)
    s = sx * w + sy
    g = gx * w + gy
    if ds.find_set(s) == ds.find_set(g):
        print('YES')
    else:
        print('NO')

param = read_data()
solve(*param)
0