結果

問題 No.157 2つの空洞
ユーザー tnodino
提出日時 2022-06-18 14:23:39
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,006 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 175,392 KB
最終ジャッジ日時 2024-10-10 03:11:39
合計ジャッジ時間 4,446 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 13 TLE * 1 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
INF = 1000
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