結果

問題 No.157 2つの空洞
ユーザー matsu7874matsu7874
提出日時 2015-12-20 12:37:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,197 bytes
コンパイル時間 236 ms
コンパイル使用メモリ 11,988 KB
実行使用メモリ 10,208 KB
最終ジャッジ日時 2023-10-17 14:12:07
合計ジャッジ時間 1,679 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,184 KB
testcase_01 AC 28 ms
10,184 KB
testcase_02 AC 28 ms
10,184 KB
testcase_03 AC 28 ms
10,184 KB
testcase_04 AC 28 ms
10,184 KB
testcase_05 AC 29 ms
10,184 KB
testcase_06 AC 28 ms
10,184 KB
testcase_07 AC 28 ms
10,184 KB
testcase_08 AC 27 ms
10,184 KB
testcase_09 AC 28 ms
10,184 KB
testcase_10 AC 28 ms
10,184 KB
testcase_11 AC 30 ms
10,188 KB
testcase_12 AC 31 ms
10,188 KB
testcase_13 AC 29 ms
10,188 KB
testcase_14 AC 30 ms
10,188 KB
testcase_15 AC 33 ms
10,188 KB
testcase_16 AC 36 ms
10,196 KB
testcase_17 AC 30 ms
10,188 KB
testcase_18 AC 35 ms
10,208 KB
testcase_19 AC 29 ms
10,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

W, H = map(int, input().split())
maze = [input() for i in range(H)]
visited = [False] * (W * H)
space = []

i = 0
while maze[i//W][i%W] == '#':
    i += 1
dq = collections.deque()
dq.append(i)

while dq:
    q = dq.popleft()
    if visited[q]:
        continue
    visited[q] = True
    space.append(q)
    for d in (-W, -1, 1, W):
        if q + d < 0 or W * H <= q + d:
            continue
        if d in (-1, 1) and (q + d) // W != q // W:
            continue
        if maze[(q+d)//W][(q+d)%W] == '.':
            dq.append(q+d)

while maze[i//W][i%W] == '#' or visited[i]:
    i += 1

visited = [W*H] * (W * H)
dq = collections.deque()
dq.append((i,0))
min_cost = W*H
while dq:
    q,v = dq.popleft()
    if q in space:
        min_cost = min(min_cost,v)
        # print(i,q,v)
        continue
    if visited[q] <= v:
        continue
    else:
        visited[q] = v
    for d in (-W, -1, 1, W):
        if q + d < 0 or W * H <= q + d:
            continue
        if d in (-1, 1) and (q + d) // W != q // W:
            continue
        if maze[(q+d)//W][(q+d)%W] == '#':
            dq.append((q+d,v+1))
        else:
            dq.append((q+d,v))
print(min_cost)
0