結果

問題 No.424 立体迷路
ユーザー brthyyjpbrthyyjp
提出日時 2021-04-26 22:21:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,727 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 77,992 KB
最終ジャッジ日時 2023-09-18 18:25:40
合計ジャッジ時間 3,735 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,172 KB
testcase_01 AC 75 ms
71,132 KB
testcase_02 AC 75 ms
71,288 KB
testcase_03 AC 77 ms
71,188 KB
testcase_04 AC 78 ms
71,376 KB
testcase_05 AC 77 ms
71,448 KB
testcase_06 AC 73 ms
71,544 KB
testcase_07 AC 74 ms
71,400 KB
testcase_08 AC 74 ms
71,172 KB
testcase_09 AC 73 ms
71,276 KB
testcase_10 AC 75 ms
71,308 KB
testcase_11 AC 76 ms
71,452 KB
testcase_12 AC 76 ms
71,556 KB
testcase_13 AC 76 ms
71,288 KB
testcase_14 AC 76 ms
71,140 KB
testcase_15 AC 76 ms
71,184 KB
testcase_16 AC 76 ms
71,528 KB
testcase_17 AC 126 ms
77,956 KB
testcase_18 AC 130 ms
77,992 KB
testcase_19 AC 125 ms
77,668 KB
testcase_20 AC 128 ms
77,412 KB
testcase_21 AC 74 ms
71,444 KB
testcase_22 AC 76 ms
71,176 KB
testcase_23 AC 74 ms
71,488 KB
testcase_24 AC 109 ms
77,372 KB
testcase_25 AC 107 ms
77,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [0]*n

    def Find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.Find(self.par[x])
            return self.par[x]

    def Unite(self, x, y):
        x = self.Find(x)
        y = self.Find(y)

        if x != y:
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x]
                self.par[x] = y
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1

    def Same(self, x, y):
        return self.Find(x) == self.Find(y)

    def Size(self, x):
        return -self.par[self.Find(x)]

h, w = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
sx, sy = sx-1, sy-1
gx, gy = gx-1, gy-1
B = []
for i in range(h):
    b = list(str(input()))
    b = [int(i) for i in b]
    B.append(b)

uf = UnionFind(h*w)
for x in range(h):
    for y in range(w):
        v = x*w+y
        for dx, dy in (-1, 0), (1, 0), (0, -1), (0, 1):
            nx, ny = x+dx, y+dy
            if 0 <= nx < h and 0 <= ny < w:
                if abs(B[x][y]-B[nx][ny]) <= 1:
                    u = nx*w+ny
                    uf.Unite(u, v)
        for dx, dy in (-2, 0), (2, 0), (0, -2), (0, 2):
            nx, ny = x+dx, y+dy
            if 0 <= nx < h and 0 <= ny < w:
                mx, my = (nx+x)//2, (ny+y)//2
                if B[x][y] == B[nx][ny] and B[mx][my] < B[x][y]:
                    u = nx*w+ny
                    uf.Unite(u, v)
s = sx*w+sy
g = gx*w+gy
if uf.Same(s, g):
    print('YES')
else:
    print('NO')
0