結果

問題 No.157 2つの空洞
ユーザー FromBooskaFromBooska
提出日時 2023-09-21 18:08:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,177 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 76,796 KB
最終ジャッジ日時 2023-09-21 18:08:56
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,408 KB
testcase_01 AC 89 ms
71,520 KB
testcase_02 AC 89 ms
71,788 KB
testcase_03 AC 92 ms
71,756 KB
testcase_04 AC 91 ms
71,696 KB
testcase_05 AC 90 ms
71,304 KB
testcase_06 AC 91 ms
71,760 KB
testcase_07 AC 99 ms
71,304 KB
testcase_08 AC 88 ms
71,556 KB
testcase_09 AC 90 ms
71,644 KB
testcase_10 AC 89 ms
71,748 KB
testcase_11 AC 91 ms
71,400 KB
testcase_12 AC 91 ms
71,748 KB
testcase_13 AC 92 ms
71,752 KB
testcase_14 AC 91 ms
71,304 KB
testcase_15 AC 98 ms
76,796 KB
testcase_16 AC 92 ms
71,508 KB
testcase_17 AC 97 ms
76,504 KB
testcase_18 AC 100 ms
71,628 KB
testcase_19 AC 90 ms
71,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# BFS distanceをやろうかと思ったがH, W小さい
# それぞれの空洞の座標をリストアウト
# 全探索して最小マンハッタン距離でいいか
# 空洞1と空洞2を決めるにはUnion Find使うか

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

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

W, H = map(int, input().split())
C = []
for i in range(H):
    C.append(input())

UF = UnionFind(W*H)
for i in range(H):
    for j in range(W):
        if j<W-1 and C[i][j] == '.' and C[i][j+1] == '.':
            UF.union(i*W+j, i*W+j+1)
        if i<H-1 and C[i][j] == '.' and C[i+1][j] == '.':
            UF.union(i*W+j, (i+1)*W+j)            

from collections import defaultdict
#print(UF.all_group_members())

groups = []
for root in UF.roots():
    if C[root//W][root%W] == '.':
        groups.append(UF.members(root))

#print(groups)

ans = 10**5
for g0 in groups[0]:
    for g1 in groups[1]:
        calc = abs(g0//W-g1//W)+abs(g0%W-g1%W)-1
        ans = min(ans, calc)
print(ans)
0