結果

問題 No.157 2つの空洞
コンテスト
ユーザー roiti46
提出日時 2015-02-26 23:32:55
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 675 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 114 ms
コンパイル使用メモリ 77,632 KB
最終ジャッジ日時 2025-12-03 14:17:24
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

dxy = zip([1,0,-1,0],[0,1,0,-1])
def rec(x,y,cnt):
    C[y][x] = str(cnt)
    for dx,dy in dxy:
        nx,ny = x+dx,y+dy
        if 0 <= nx < W and 0 <= ny < H and C[ny][nx] == ".":
            rec(nx,ny,cnt)
            
W,H = map(int,raw_input().split())
C = [list(raw_input()) for i in xrange(H)]
cnt = 0
for y in xrange(H):
    for x in xrange(W):
        if C[y][x] == ".":
            rec(x,y,cnt)
            cnt += 1
ans = 100000
for y1 in xrange(H):
    for x1 in xrange(W):
        for y2 in xrange(H):
            for x2 in xrange(W):
                if C[y1][x1] == "0" and C[y2][x2] == "1":
                    ans = min(ans, abs(x1-x2)+abs(y1-y2))
print ans-1
0