結果

問題 No.157 2つの空洞
ユーザー tnodinotnodino
提出日時 2022-06-18 14:25:17
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,011 bytes
コンパイル時間 572 ms
コンパイル使用メモリ 82,360 KB
実行使用メモリ 769,228 KB
最終ジャッジ日時 2024-04-18 10:00:44
合計ジャッジ時間 5,085 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
62,060 KB
testcase_01 AC 40 ms
54,516 KB
testcase_02 AC 39 ms
54,292 KB
testcase_03 AC 40 ms
55,328 KB
testcase_04 AC 39 ms
55,228 KB
testcase_05 AC 39 ms
54,536 KB
testcase_06 AC 41 ms
54,116 KB
testcase_07 AC 38 ms
54,668 KB
testcase_08 AC 39 ms
54,860 KB
testcase_09 AC 41 ms
54,000 KB
testcase_10 AC 39 ms
54,188 KB
testcase_11 AC 40 ms
55,200 KB
testcase_12 AC 42 ms
55,712 KB
testcase_13 AC 41 ms
55,608 KB
testcase_14 AC 41 ms
55,244 KB
testcase_15 AC 58 ms
68,952 KB
testcase_16 AC 48 ms
62,732 KB
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
INF = 500
W,H = map(int,input().split())
C = [list(input()) for _ in range(H)]
x = 0
y = 0
for i in range(H):
    for j in range(W):
        if C[i][j] == '.':
            x = i
            y = j
Queue = deque()
Queue2 = deque()
Queue2.append((x, y))
cost = [[INF] * W for _ in range(H)]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
while Queue2:
    x,y = Queue2.popleft()
    cost[x][y] = 0
    Queue.append((x, y))
    for i in range(4):
        nx,ny = x+dx[i],y+dy[i]
        if C[nx][ny] == '.' and cost[nx][ny] == INF:
            Queue2.append((nx, ny))
ans = INF
while Queue:
    x,y = Queue.popleft()
    for i in range(4):
        nx,ny = x+dx[i],y+dy[i]
        if nx <= 0 or H-1 <= nx or ny <= 0 or W-1 <= ny:
            continue
        if C[nx][ny] == '#' and cost[x][y] + 1 < cost[nx][ny]:
            cost[nx][ny] = cost[x][y] + 1
            Queue.append((nx, ny))
        elif C[nx][ny] == '.' and cost[nx][ny] == INF:
            ans = min(ans, cost[x][y])
print(ans)
0