結果
問題 | No.157 2つの空洞 |
ユーザー | aka_satana_ha |
提出日時 | 2017-11-27 15:17:13 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 36 ms / 2,000 ms |
コード長 | 2,855 bytes |
コンパイル時間 | 120 ms |
コンパイル使用メモリ | 13,312 KB |
実行使用メモリ | 11,392 KB |
最終ジャッジ日時 | 2024-05-05 13:18:05 |
合計ジャッジ時間 | 1,527 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
11,136 KB |
testcase_01 | AC | 35 ms
11,136 KB |
testcase_02 | AC | 32 ms
11,136 KB |
testcase_03 | AC | 31 ms
11,136 KB |
testcase_04 | AC | 36 ms
11,008 KB |
testcase_05 | AC | 32 ms
11,136 KB |
testcase_06 | AC | 32 ms
11,136 KB |
testcase_07 | AC | 32 ms
11,136 KB |
testcase_08 | AC | 32 ms
11,008 KB |
testcase_09 | AC | 31 ms
11,008 KB |
testcase_10 | AC | 31 ms
11,008 KB |
testcase_11 | AC | 32 ms
11,008 KB |
testcase_12 | AC | 31 ms
11,136 KB |
testcase_13 | AC | 32 ms
11,136 KB |
testcase_14 | AC | 32 ms
11,136 KB |
testcase_15 | AC | 33 ms
11,264 KB |
testcase_16 | AC | 31 ms
11,264 KB |
testcase_17 | AC | 35 ms
11,392 KB |
testcase_18 | AC | 32 ms
11,136 KB |
testcase_19 | AC | 31 ms
11,136 KB |
ソースコード
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)