結果

問題 No.157 2つの空洞
ユーザー aka_satana_haaka_satana_ha
提出日時 2017-11-27 15:17:13
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 2,855 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 11,240 KB
実行使用メモリ 8,604 KB
最終ジャッジ日時 2023-08-18 07:21:59
合計ジャッジ時間 1,564 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,376 KB
testcase_01 AC 17 ms
8,420 KB
testcase_02 AC 17 ms
8,376 KB
testcase_03 AC 17 ms
8,488 KB
testcase_04 AC 17 ms
8,364 KB
testcase_05 AC 17 ms
8,372 KB
testcase_06 AC 17 ms
8,452 KB
testcase_07 AC 17 ms
8,432 KB
testcase_08 AC 17 ms
8,372 KB
testcase_09 AC 17 ms
8,384 KB
testcase_10 AC 16 ms
8,368 KB
testcase_11 AC 17 ms
8,492 KB
testcase_12 AC 17 ms
8,336 KB
testcase_13 AC 17 ms
8,388 KB
testcase_14 AC 17 ms
8,316 KB
testcase_15 AC 18 ms
8,372 KB
testcase_16 AC 17 ms
8,344 KB
testcase_17 AC 19 ms
8,604 KB
testcase_18 AC 17 ms
8,340 KB
testcase_19 AC 17 ms
8,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

W,H=[int(i) for i in input().split()]
C=[]
for i in range(H):
    line=input()
    C.append(line)

# print(C)

#一つ目の穴の検索
ana1=[]
for i in range(H):
    find=False
    for j in range(W):
        if C[i][j]=='.':
            ana1.append([i,j])
            find=True
            break
    if find==True:
        break

#深さ優先探索
def ana1_dfs(now):
    y=now[0]
    x=now[1]
    if C[y+1][x]=="." and [y+1,x] not in ana1:
        ana1.append([y+1,x])
        # C[y+1][x]="#"
        line=C[y+1]
        line=line[:x]+'#'+line[x+1:]
        C[y+1]=line
        ana1_dfs([y+1,x])
    if C[y][x+1]=="." and [y,x+1] not in ana1:
        ana1.append([y,x+1])
        # C[y][x+1]="#"
        line=C[y]
        line=line[:x+1]+'#'+line[x+2:]
        C[y]=line
        ana1_dfs([y,x+1])
    if C[y-1][x]=="." and [y-1,x] not in ana1:
        ana1.append([y-1,x])
        # C[y+1][x]="#"
        line=C[y-1]
        line=line[:x]+'#'+line[x+1:]
        C[y-1]=line
        ana1_dfs([y-1,x])
    if C[y][x-1]=="." and [y,x-1] not in ana1:
        ana1.append([y,x-1])
        # C[y][x+1]="#"
        line=C[y]
        line=line[:x-1]+'#'+line[x:]
        C[y]=line
        ana1_dfs([y,x-1])


y=ana1[-1][0]
x=ana1[-1][1]
line=C[y]
line=line[:x]+'#'+line[x+1:]
C[y]=line
ana1_dfs(ana1[-1])

# print(ana1)

#ふたつ目の穴の検索
ana2=[]
for i in range(H):
    find=False
    for j in range(W):
        if C[i][j]=='.':
            ana2.append([i,j])
            find=True
            break
    if find==True:
        break

#左上から右下へ深さ優先探索
def ana2_dfs(now):
    y=now[0]
    x=now[1]
    if C[y+1][x]=="." and [y+1,x] not in ana2:
        ana2.append([y+1,x])
        # C[y+1][x]="#"
        line=C[y+1]
        line=line[:x]+'#'+line[x+1:]
        C[y+1]=line
        ana2_dfs([y+1,x])
    if C[y][x+1]=="." and [y,x+1] not in ana2:
        ana2.append([y,x+1])
        # C[y][x+1]="#"
        line=C[y]
        line=line[:x+1]+'#'+line[x+2:]
        C[y]=line
        ana2_dfs([y,x+1])
    if C[y-1][x]=="." and [y-1,x] not in ana2:
        ana2.append([y-1,x])
        # C[y+1][x]="#"
        line=C[y-1]
        line=line[:x]+'#'+line[x+1:]
        C[y-1]=line
        ana2_dfs([y-1,x])
    if C[y][x-1]=="." and [y,x-1] not in ana2:
        ana2.append([y,x-1])
        # C[y][x+1]="#"
        line=C[y]
        line=line[:x-1]+'#'+line[x:]
        C[y]=line
        ana2_dfs([y,x-1])

y=ana2[-1][0]
x=ana2[-1][1]
line=C[y]
line=line[:x]+'#'+line[x+1:]
C[y]=line
ana2_dfs(ana2[-1])

# print(ana2)

#ana1とana2の各要素の距離で最小のものを検索
tmp_min=H+W
for ana1_mass in ana1:
    for ana2_mass in ana2:
        dist=abs(ana1_mass[0]-ana2_mass[0])+abs(ana1_mass[1]-ana2_mass[1])-1
        # print(ana1_mass, ana2_mass, dist)
        if dist<tmp_min:
            tmp_min=dist
print(tmp_min)
0