結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-03-27 03:30:23 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 38 ms / 2,000 ms |
| コード長 | 1,219 bytes |
| コンパイル時間 | 80 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 11,392 KB |
| 最終ジャッジ日時 | 2024-06-29 01:24:54 |
| 合計ジャッジ時間 | 1,715 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
import queue
import math
W,H = [int(x) for x in input().split(" ")]
def Find(i,j,p,l):
que = queue.Queue()
tmp = []
que.put([i,j])
tmp.append([i,j])
tmplist = list(l[i])
tmplist[j] = '#'
l[i] = "".join(tmplist)
dx = [-1,0,0,1]
dy = [0,-1,1,0]
while not que.empty():
t = que.get()
for i in range(len(dx)):
x = dx[i]
y = dy[i]
if t[0] + x < 0 or t[0] + x >= H or t[1] + y < 0 or t[1] + y >= W:
continue
if l[t[0]+x][t[1]+y] == '.':
que.put([t[0]+x,t[1]+y])
tmp.append([t[0]+x,t[1]+y])
tmplist = list(l[t[0]+x])
tmplist[t[1]+y] = '#'
l[t[0]+x] = "".join(tmplist)
p.put(tmp)
return [p,l]
def Calc(p):
p1 = p.get()
p2 = p.get()
m = 2000000000
for x in p1:
for y in p2:
m = min(m,math.fabs(x[0]-y[0])+math.fabs(x[1]-y[1])-1)
return int(m)
l = []
for i in range(H):
l.append(input())
p = queue.Queue()
for i in range(H):
for j in range(W):
if l[i][j] == '.':
p,l = Find(i,j,p,l)
res = Calc(p)
print(res)