結果

問題 No.157 2つの空洞
ユーザー DrDrpilotDrDrpilot
提出日時 2022-06-07 14:56:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 654 bytes
コンパイル時間 482 ms
コンパイル使用メモリ 11,912 KB
実行使用メモリ 10,216 KB
最終ジャッジ日時 2023-10-21 03:49:57
合計ジャッジ時間 2,248 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

w,h=map(int,input().split())
g=[list(input()) for _ in range(h)]
cave1=[];cave2=[]
def dfs(y,x,lis):
    g[y][x]='#'
    lis.append((y,x))
    dy=[0,1,0,-1];dx=[1,0,-1,0]
    for i in range(4):
        ny=y+dy[i];nx=x+dx[i]
        if not (0<=ny<h and 0<=nx<w):
            continue
        if g[ny][nx]=='.':
            dfs(ny,nx,lis)
first=True
for i in range(h):
    for j in range(w):
        if g[i][j]=='.':
            if first:
                dfs(i,j,cave1)
                first=False
            else:
                dfs(i,j,cave2)
ans=1<<60
for y1,x1 in cave1:
    for y2,x2 in cave2:
        ans=min(ans,abs(y2-y1)+abs(x2-x1)-1)
print(ans)
0