結果

問題 No.157 2つの空洞
ユーザー brthyyjpbrthyyjp
提出日時 2022-03-21 14:04:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,059 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 66,688 KB
最終ジャッジ日時 2024-04-17 07:07:40
合計ジャッジ時間 2,019 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,760 KB
testcase_01 AC 46 ms
54,272 KB
testcase_02 AC 50 ms
54,144 KB
testcase_03 AC 47 ms
54,272 KB
testcase_04 AC 48 ms
54,144 KB
testcase_05 AC 48 ms
54,400 KB
testcase_06 AC 50 ms
54,272 KB
testcase_07 AC 49 ms
54,144 KB
testcase_08 AC 49 ms
54,144 KB
testcase_09 AC 48 ms
54,528 KB
testcase_10 AC 46 ms
53,888 KB
testcase_11 AC 49 ms
54,400 KB
testcase_12 AC 49 ms
54,528 KB
testcase_13 AC 53 ms
60,160 KB
testcase_14 AC 60 ms
62,464 KB
testcase_15 AC 58 ms
61,824 KB
testcase_16 AC 56 ms
61,696 KB
testcase_17 AC 66 ms
66,688 KB
testcase_18 AC 57 ms
61,568 KB
testcase_19 AC 56 ms
61,184 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)]

from collections import deque
INF = 10**18

w, h = map(int, input().split())
C = [str(input()) for i in range(h)]
uf = UnionFind(h*w)
for y in range(h):
    for x in range(w):
        if C[y][x] == '#':
            continue
        for dy, dx in (-1, 0), (1, 0), (0, 1), (0, -1):
            ny, nx = y+dy, x+dx
            if 0 <= ny < h and 0 <= nx < w:
                if C[ny][nx] != '#':
                    uf.Unite(y*w+x, ny*w+nx)

for y in range(h):
    for x in range(w):
        if C[y][x] != '#':
            p = y*w+x
            break
    else:
        continue
    break

q = deque([])
dist = [[INF]*w for i in range(h)]
for y in range(h):
    for x in range(w):
        if C[y][x] == '#':
            continue
        if uf.Same(p, y*w+x):
            q.append((y, x))
            dist[y][x] = 0
while q:
    y, x = q.popleft()
    for dy, dx in (-1, 0), (1, 0), (0, 1), (0, -1):
        ny, nx = y+dy, x+dx
        if 0 <= ny < h and 0 <= nx < w:
            if dist[ny][nx] == INF:
                dist[ny][nx] = dist[y][x]+1
                q.append((ny, nx))
ans = INF
for y in range(h):
    for x in range(w):
        if C[y][x] == '#':
            continue
        if not uf.Same(p, y*w+x):
            ans = min(ans, dist[y][x]-1)
print(ans)
0