結果

問題 No.157 2つの空洞
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-02 20:19:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 11,136 KB
実行使用メモリ 8,904 KB
最終ジャッジ日時 2023-09-20 18:51:48
合計ジャッジ時間 1,454 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,752 KB
testcase_01 AC 20 ms
8,832 KB
testcase_02 AC 20 ms
8,820 KB
testcase_03 AC 20 ms
8,680 KB
testcase_04 AC 19 ms
8,820 KB
testcase_05 AC 19 ms
8,872 KB
testcase_06 AC 19 ms
8,848 KB
testcase_07 AC 19 ms
8,704 KB
testcase_08 AC 19 ms
8,788 KB
testcase_09 AC 19 ms
8,868 KB
testcase_10 AC 20 ms
8,716 KB
testcase_11 AC 20 ms
8,680 KB
testcase_12 AC 20 ms
8,872 KB
testcase_13 AC 20 ms
8,688 KB
testcase_14 AC 20 ms
8,732 KB
testcase_15 AC 20 ms
8,720 KB
testcase_16 AC 20 ms
8,864 KB
testcase_17 AC 21 ms
8,904 KB
testcase_18 AC 20 ms
8,880 KB
testcase_19 AC 20 ms
8,768 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():
    W, H = map(int, input().split())
    maze = [input().strip() for h in range(H)]
    return W, H, maze

import collections

def solve(W, H, maze):
    maze = [[c == '.' for c in row] for row in maze]
    djs = DisjointSet(W * H)
    for h in range(1, H-1):
        for w in range(1, W-1):
            pos = h * W + w
            if maze[h][w]:
                if maze[h-1][w]:
                    djs.union(pos, pos - W)
                if maze[h][w-1]:
                    djs.union(pos, pos - 1)
            else:
                djs.union(pos, 0)
    areas = collections.defaultdict(list)
    for h in range(1, H-1):
        for w in range(1, W-1):
            p = djs.find_set(h * W + w)
            if p:
                areas[p].append((h, w))
    a1, a2 = areas
    frontiers = areas[a1]
    dist = 0
    while frontiers:
        new_frontiers = []
        for (h, w) in frontiers:
            for nh, nw in [(h+1, w), (h-1, w), (h, w+1), (h, w-1)]:
                if nh < 0 or nh >= H or nw < 0 or nw >= W:
                    continue
                if djs.find_set(nh * W + nw) == a2:
                    return dist
                elif not maze[nh][nw]:
                    maze[nh][nw] = True
                    new_frontiers.append((nh, nw))
        frontiers = new_frontiers
        dist += 1

if __name__ == '__main__':
    W, H, maze = read_data()
    print(solve(W, H, maze))
0