結果

問題 No.157 2つの空洞
ユーザー yuki2006yuki2006
提出日時 2015-02-26 19:32:40
言語 PyPy2
(7.3.13)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 77,828 KB
実行使用メモリ 80,032 KB
最終ジャッジ日時 2023-09-06 05:06:52
合計ジャッジ時間 3,568 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
76,480 KB
testcase_01 AC 82 ms
76,136 KB
testcase_02 AC 80 ms
77,340 KB
testcase_03 AC 87 ms
78,244 KB
testcase_04 AC 90 ms
78,884 KB
testcase_05 AC 93 ms
79,300 KB
testcase_06 AC 92 ms
78,912 KB
testcase_07 AC 87 ms
78,920 KB
testcase_08 AC 85 ms
78,756 KB
testcase_09 AC 95 ms
79,016 KB
testcase_10 AC 85 ms
78,600 KB
testcase_11 AC 93 ms
78,652 KB
testcase_12 AC 95 ms
78,680 KB
testcase_13 AC 101 ms
78,936 KB
testcase_14 AC 105 ms
78,884 KB
testcase_15 AC 114 ms
79,720 KB
testcase_16 AC 106 ms
79,260 KB
testcase_17 AC 123 ms
80,032 KB
testcase_18 AC 107 ms
78,868 KB
testcase_19 AC 99 ms
79,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

W, H = map(int, raw_input().split())

C = ["" for _ in xrange(H)]

matrix = [[0 for i in xrange(W)] for i in xrange(H)]


# close range
def range_check(i, mn, mx):
    return mn <= i < mx


def euclidean_distance(x, y, length):
    return abs(x) + abs(y) <= length


def dfs(C, matrix, x, y, current):
    mn = H * W

    if C[y][x] == "#" or matrix[y][x] != 0:
        return 0

    matrix[y][x] = current

    for i in xrange(-1, 2):
        for j in xrange(-1, 2):
            if i == 0 and j == 0:
                continue

            if not (range_check(x + i, 0, W) and range_check(y + j, 0, H)):
                continue
            if not euclidean_distance(i, j, 1):
                continue
            result = dfs(C, matrix, x + i, y + j, current)
            mn = min(result, mn)

    return mn


for i in xrange(H):
    C[i] = raw_input()

current = 1
for i in xrange(H):
    for j in xrange(W):
        if C[i][j] == "." and matrix[i][j] == 0:
            dfs(C, matrix, j, i, current)
            current += 1

mn = W * H
for j1 in xrange(H):
    for i1 in xrange(W):
        for j2 in xrange(H):
            for i2 in xrange(W):
                if i1 == i2 and j1 == j2:
                    continue
                if C[j1][i1] == "#" or C[j2][i2] == "#":
                    continue
                if matrix[j1][i1] == matrix[j2][i2]:
                    continue
                mn = min(mn, abs(i1 - i2) + abs(j1 - j2) - 1)

print mn
0