結果

問題 No.157 2つの空洞
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-02-29 20:42:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 72,804 KB
最終ジャッジ日時 2024-09-24 12:52:44
合計ジャッジ時間 2,301 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
62,472 KB
testcase_01 AC 46 ms
62,952 KB
testcase_02 AC 45 ms
62,444 KB
testcase_03 AC 46 ms
62,768 KB
testcase_04 AC 46 ms
63,368 KB
testcase_05 AC 47 ms
62,712 KB
testcase_06 AC 51 ms
63,168 KB
testcase_07 AC 47 ms
61,756 KB
testcase_08 AC 46 ms
61,780 KB
testcase_09 AC 46 ms
62,468 KB
testcase_10 AC 45 ms
61,964 KB
testcase_11 AC 50 ms
64,452 KB
testcase_12 AC 55 ms
65,000 KB
testcase_13 AC 52 ms
66,076 KB
testcase_14 AC 61 ms
69,876 KB
testcase_15 AC 65 ms
72,360 KB
testcase_16 AC 65 ms
72,804 KB
testcase_17 AC 67 ms
72,792 KB
testcase_18 AC 54 ms
67,012 KB
testcase_19 AC 63 ms
69,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3
# -*- coding: utf-8 -*-

import array
import collections
import copy
import heapq
import itertools
import sys


DELTA = [(1, 0), (-1, 0), (0, 1), (0, -1)]
INF = 10 ** 4
IS_HOLLOW = True
IS_WALL = False


class RecursiveDepthFirstSearch(object):

    def __init__(self, width, height, stage):
        self.width = width
        self.height = height
        self.stage = stage
        self.hollow_representatives = []

    def search(self, r0, c0):
        if r0 < 0 or r0 >= self.height or c0 < 0 or c0 >= self.width:
            pass
        elif self.stage[r0][c0] == IS_WALL:
            pass
        else:
            self.stage[r0][c0] = IS_WALL
            for dr, dc in DELTA:
                self.search(r0 + dr, c0 + dc)

    def find_hollow_representatives(self):
        for r, c in itertools.product(range(self.height), range(self.width)):
            if self.stage[r][c] == IS_HOLLOW:
                self.hollow_representatives.append((r, c))
                self.search(r, c)
        return self.hollow_representatives


class Dijkstra(object):

    def __init__(self, width, height, stage, start, goal):
        self.width = width
        self.height = height
        self.stage = stage
        self.start = start
        self.goal = goal
        self.dist = None

    def init_dist(self):
        self.dist = collections.defaultdict(lambda: INF)
        self.dist[self.start] = 0

    def shortest_path_to_goal(self):
        self.init_dist()
        pq = [(self.dist[(r, c)], (r, c))
              for r, c in itertools.product(range(self.height), range(self.width))]
        heapq.heapify(pq)
        while pq:
            (_, (r0, c0)) = heapq.heappop(pq)
            if (r0, c0) == self.goal:
                break
            for dr, dc in DELTA:
                (r, c) = (r0 + dr, c0 + dc)
                if r < 0 or r >= self.height or c < 0 or c >= self.width:
                    continue
                else:
                    if (not self.stage[r0][c0]) and (not self.stage[r][c]):
                        cost = 1
                    else:
                        cost = 0
                    new_length = self.dist[(r0, c0)] + cost
                    if new_length < self.dist[(r, c)]:
                        self.dist[(r, c)] = new_length
                        heapq.heappush(pq, (new_length, (r, c)))
        return self.dist[self.goal]


def main():
    sys.setrecursionlimit(10000)
    f = lambda c: c == "."
    w, h = map(int, input().split())
    stage = [array.array("B", map(f, input())) for _ in range(h)]
    dfs = RecursiveDepthFirstSearch(w, h, copy.deepcopy(stage))
    start, goal = dfs.find_hollow_representatives()
    dij = Dijkstra(w, h, stage, start, goal)
    print(dij.shortest_path_to_goal() + 1)


if __name__ == '__main__':
    main()
0