結果

問題 No.157 2つの空洞
ユーザー flippergoflippergo
提出日時 2020-12-22 11:22:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,528 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 81,760 KB
実行使用メモリ 68,212 KB
最終ジャッジ日時 2023-10-21 12:46:35
合計ジャッジ時間 2,551 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,568 KB
testcase_01 AC 44 ms
55,568 KB
testcase_02 AC 44 ms
55,568 KB
testcase_03 AC 39 ms
55,568 KB
testcase_04 AC 41 ms
55,568 KB
testcase_05 AC 42 ms
55,568 KB
testcase_06 AC 42 ms
55,568 KB
testcase_07 AC 42 ms
55,568 KB
testcase_08 AC 40 ms
55,568 KB
testcase_09 AC 40 ms
55,568 KB
testcase_10 AC 43 ms
55,568 KB
testcase_11 AC 39 ms
55,568 KB
testcase_12 AC 40 ms
55,568 KB
testcase_13 AC 43 ms
55,568 KB
testcase_14 AC 53 ms
63,884 KB
testcase_15 AC 58 ms
68,184 KB
testcase_16 AC 59 ms
68,168 KB
testcase_17 AC 50 ms
64,012 KB
testcase_18 AC 61 ms
68,212 KB
testcase_19 AC 42 ms
55,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
W,H = map(int,input().split())
C = [input().strip() for _ in range(H)]
A = [[100 for _ in range(W)] for _ in range(H)]
flag = 0
for i in range(H):
    for j in range(W):
        if C[i][j]==".":
            i0 = i
            j0 = j
            flag = 1
            break
    if flag==1:break
que = deque([(i0,j0,0)])
A[i0][j0]=0
while que:
    y,x,d = que.popleft()
    if x+1<W and C[y][x+1]==".":
        if A[y][x+1]>d:
            A[y][x+1]=d
            que.append((y,x+1,A[y][x+1]))
    elif x+1<W and C[y][x+1]=="#":
        if A[y][x+1]>d+1:
            A[y][x+1]=d+1
            que.append((y,x+1,d+1))
    if y-1>=0 and C[y-1][x]==".":
        if A[y-1][x]>d:
            A[y-1][x]=d
            que.append((y-1,x,d))
    elif y-1>=0 and C[y-1][x]=="#":
        if A[y-1][x]>d+1:
            A[y-1][x]=d+1
            que.append((y-1,x,d+1))
    if x-1>=0 and C[y][x-1]==".":
        if A[y][x-1]>d:
            A[y][x-1]=d
            que.append((y,x-1,d))
    elif x-1>=0 and C[y][x-1]=="#":
        if A[y][x-1]>d+1:
            A[y][x-1]=d+1
            que.append((y,x-1,d+1))
    if y+1<H and C[y+1][x]==".":
        if A[y+1][x]>d:
            A[y+1][x]=d
            que.append((y+1,x,d))
    elif y+1<H and C[y+1][x]=="#":
        if A[y+1][x]>d+1:
            A[y+1][x]=d+1
            que.append((y+1,x,d+1))
dmin = -1
for i in range(H):
    for j in range(W):
        if C[i][j]=="." and A[i][j]>0:
            dmin = A[i][j]
            break
    if dmin>0:break
print(dmin)
0