結果

問題 No.157 2つの空洞
ユーザー gorugo30gorugo30
提出日時 2021-03-16 22:38:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 924 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 65,024 KB
最終ジャッジ日時 2024-04-26 05:29:52
合計ジャッジ時間 1,949 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
52,480 KB
testcase_01 AC 43 ms
52,736 KB
testcase_02 AC 42 ms
53,248 KB
testcase_03 AC 40 ms
52,992 KB
testcase_04 AC 39 ms
53,504 KB
testcase_05 AC 40 ms
52,992 KB
testcase_06 AC 41 ms
53,248 KB
testcase_07 AC 39 ms
52,864 KB
testcase_08 AC 40 ms
53,248 KB
testcase_09 AC 44 ms
52,992 KB
testcase_10 AC 43 ms
53,248 KB
testcase_11 AC 42 ms
53,504 KB
testcase_12 AC 43 ms
53,760 KB
testcase_13 AC 51 ms
62,336 KB
testcase_14 AC 51 ms
61,696 KB
testcase_15 AC 53 ms
63,104 KB
testcase_16 AC 55 ms
63,700 KB
testcase_17 AC 55 ms
63,744 KB
testcase_18 AC 56 ms
65,024 KB
testcase_19 AC 56 ms
64,640 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