結果

問題 No.157 2つの空洞
ユーザー tnodinotnodino
提出日時 2022-06-18 14:23:39
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,006 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 185,372 KB
最終ジャッジ日時 2024-04-18 09:58:48
合計ジャッジ時間 4,286 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
17,824 KB
testcase_01 AC 33 ms
10,752 KB
testcase_02 AC 31 ms
10,752 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 27 ms
10,752 KB
testcase_06 AC 27 ms
10,880 KB
testcase_07 AC 33 ms
10,752 KB
testcase_08 AC 29 ms
10,880 KB
testcase_09 AC 29 ms
10,880 KB
testcase_10 AC 27 ms
10,880 KB
testcase_11 AC 31 ms
10,880 KB
testcase_12 AC 28 ms
10,880 KB
testcase_13 AC 30 ms
11,008 KB
testcase_14 AC 28 ms
10,752 KB
testcase_15 AC 33 ms
10,880 KB
testcase_16 AC 28 ms
10,752 KB
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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