結果

問題 No.157 2つの空洞
ユーザー gorugo30gorugo30
提出日時 2021-03-16 22:38:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 924 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 76,976 KB
最終ジャッジ日時 2023-08-08 12:34:58
合計ジャッジ時間 3,334 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,424 KB
testcase_01 AC 76 ms
71,504 KB
testcase_02 AC 77 ms
71,412 KB
testcase_03 AC 78 ms
71,484 KB
testcase_04 AC 79 ms
71,352 KB
testcase_05 AC 78 ms
71,428 KB
testcase_06 AC 79 ms
71,500 KB
testcase_07 AC 78 ms
71,360 KB
testcase_08 AC 78 ms
71,504 KB
testcase_09 AC 78 ms
71,168 KB
testcase_10 AC 79 ms
71,460 KB
testcase_11 AC 80 ms
71,644 KB
testcase_12 AC 80 ms
71,412 KB
testcase_13 AC 90 ms
76,760 KB
testcase_14 AC 90 ms
76,224 KB
testcase_15 AC 92 ms
76,856 KB
testcase_16 AC 89 ms
76,756 KB
testcase_17 AC 92 ms
76,912 KB
testcase_18 AC 93 ms
76,976 KB
testcase_19 AC 92 ms
76,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

W, H = map(int, input().split())
grid = [input() for i in range(H)]
dist = [[-1] * W for i in range(H)]

import heapq
dr = [-1, 0, 0, 1]
dc = [0, -1, 1, 0]

sr = -1
sc = -1
for r in range(H):
    for c in range(W):
        if grid[r][c] == ".":
            sr = r
            sc = c
            break
    if sr != -1:
        break

priq = [(0, sr, sc)]
dist[sr][sc] = 0
while len(priq):
    d, r, c = heapq.heappop(priq)
    if d > dist[r][c]:
        continue
    for i in range(4):
        nr = r + dr[i]
        nc = c + dc[i]
        if not (0 <= nr < H and 0 <= nc < W):
            continue
        nd = d
        if grid[nr][nc] == "#":
            nd += 1
        if dist[nr][nc] == -1 or dist[nr][nc] > nd:
            dist[nr][nc] = nd
            heapq.heappush(priq, (nd, nr, nc))

ans = 0
for i in range(H):
    for j in range(W):
        if grid[i][j] == ".":
            ans = max(ans, dist[i][j])
print(ans)
0