結果

問題 No.157 2つの空洞
ユーザー tnodinotnodino
提出日時 2022-06-18 14:23:18
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,007 bytes
コンパイル時間 225 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 618,580 KB
最終ジャッジ日時 2024-04-18 09:58:12
合計ジャッジ時間 5,074 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
59,392 KB
testcase_01 AC 41 ms
54,400 KB
testcase_02 AC 40 ms
54,016 KB
testcase_03 AC 41 ms
54,400 KB
testcase_04 AC 40 ms
53,888 KB
testcase_05 AC 41 ms
54,272 KB
testcase_06 AC 44 ms
54,456 KB
testcase_07 AC 40 ms
54,144 KB
testcase_08 AC 41 ms
54,144 KB
testcase_09 AC 41 ms
54,016 KB
testcase_10 AC 40 ms
54,144 KB
testcase_11 AC 42 ms
54,272 KB
testcase_12 AC 42 ms
54,784 KB
testcase_13 AC 47 ms
61,568 KB
testcase_14 AC 42 ms
54,272 KB
testcase_15 AC 58 ms
66,560 KB
testcase_16 AC 49 ms
62,336 KB
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
INF = 1<<16
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 <= nx or ny < 0 or W <= 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