結果

問題 No.157 2つの空洞
ユーザー ynyn
提出日時 2015-09-22 02:08:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 8,188 KB
最終ジャッジ日時 2023-09-26 14:20:05
合計ジャッジ時間 1,591 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,828 KB
testcase_01 AC 16 ms
7,696 KB
testcase_02 AC 16 ms
7,728 KB
testcase_03 AC 16 ms
7,692 KB
testcase_04 AC 16 ms
7,728 KB
testcase_05 AC 16 ms
7,848 KB
testcase_06 AC 16 ms
7,788 KB
testcase_07 AC 16 ms
7,852 KB
testcase_08 AC 16 ms
7,748 KB
testcase_09 AC 16 ms
7,732 KB
testcase_10 AC 16 ms
7,824 KB
testcase_11 AC 17 ms
7,816 KB
testcase_12 AC 17 ms
7,908 KB
testcase_13 AC 17 ms
7,696 KB
testcase_14 AC 22 ms
7,792 KB
testcase_15 AC 21 ms
7,796 KB
testcase_16 AC 19 ms
7,732 KB
testcase_17 AC 33 ms
8,188 KB
testcase_18 AC 17 ms
7,760 KB
testcase_19 AC 16 ms
7,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

w, h = map(int,input().split())
c = [list(input()) for j in range(h)]

dx = [ 1, 0,-1, 0]
dy = [ 0, 1, 0,-1]

nx = 0
ny = 0

cnt = 0

def dfs(x,y,count):
    c[y][x] = str(count)
    for k in range(4):
        if c[y+dy[k]][x+dx[k]] == ".":
            dfs(x+dx[k], y+dy[k], count)

for j in range(h):
    for i in range(w):
        if c[j][i] == ".":
            dfs(i, j, cnt)
            cnt += 1

ans = 10**9
for j in range(h):
    for i in range(w):
        if c[j][i] == "0":
            for l in range(h):
                for k in range(w):
                    if c[l][k] == "1":
                        ans = min(ans, abs(j-l) + abs(i-k))

print(ans-1)
0