結果

問題 No.402 最も海から遠い場所
ユーザー titiatitia
提出日時 2023-03-20 01:47:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,904 ms / 3,000 ms
コード長 753 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 81,872 KB
実行使用メモリ 451,228 KB
最終ジャッジ日時 2023-10-18 17:59:28
合計ジャッジ時間 14,784 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,500 KB
testcase_01 AC 41 ms
55,500 KB
testcase_02 AC 68 ms
70,576 KB
testcase_03 AC 41 ms
55,500 KB
testcase_04 AC 41 ms
55,500 KB
testcase_05 AC 45 ms
61,524 KB
testcase_06 AC 41 ms
55,500 KB
testcase_07 AC 41 ms
55,500 KB
testcase_08 AC 42 ms
55,500 KB
testcase_09 AC 46 ms
61,524 KB
testcase_10 AC 43 ms
55,500 KB
testcase_11 AC 62 ms
68,488 KB
testcase_12 AC 58 ms
66,344 KB
testcase_13 AC 98 ms
77,280 KB
testcase_14 AC 87 ms
76,464 KB
testcase_15 AC 157 ms
85,812 KB
testcase_16 AC 181 ms
89,648 KB
testcase_17 AC 1,386 ms
256,852 KB
testcase_18 AC 2,904 ms
287,568 KB
testcase_19 AC 2,465 ms
451,228 KB
testcase_20 AC 2,601 ms
252,764 KB
testcase_21 AC 2,416 ms
411,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque

H,W=map(int,input().split())
MAP=[input() for i in range(H)]

ANS=[[1<<30]*(W+2) for i in range(H+2)]

Q=deque()

for i in range(H+2):
    ANS[i][0]=0
    ANS[i][W+1]=0
    Q.append((i,0))
    Q.append((i,W+1))


for i in range(W+2):
    ANS[0][i]=0
    ANS[H+1][i]=0
    Q.append((0,i))
    Q.append((H+1,i))

for i in range(H):
    for j in range(W):
        if MAP[i][j]==".":
            Q.append((i+1,j+1))
            ANS[i+1][j+1]=0

while Q:
    x,y=Q.popleft()
    for z in range(x-1,x+2):
        for w in range(y-1,y+2):
            if 0<=z<H+2 and 0<=w<W+2 and ANS[z][w]>ANS[x][y]+1:
                ANS[z][w]=ANS[x][y]+1
                Q.append((z,w))

print(ANS[x][y])
0