結果

問題 No.157 2つの空洞
ユーザー nanaenanae
提出日時 2017-02-16 16:57:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,472 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 8,740 KB
最終ジャッジ日時 2023-08-28 17:39:24
合計ジャッジ時間 1,430 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,712 KB
testcase_01 AC 20 ms
8,720 KB
testcase_02 AC 19 ms
8,616 KB
testcase_03 AC 20 ms
8,636 KB
testcase_04 AC 19 ms
8,652 KB
testcase_05 AC 20 ms
8,640 KB
testcase_06 AC 19 ms
8,624 KB
testcase_07 AC 19 ms
8,740 KB
testcase_08 AC 19 ms
8,680 KB
testcase_09 AC 20 ms
8,576 KB
testcase_10 AC 20 ms
8,628 KB
testcase_11 AC 19 ms
8,628 KB
testcase_12 AC 19 ms
8,624 KB
testcase_13 AC 19 ms
8,720 KB
testcase_14 AC 19 ms
8,636 KB
testcase_15 AC 20 ms
8,640 KB
testcase_16 AC 20 ms
8,720 KB
testcase_17 AC 20 ms
8,644 KB
testcase_18 AC 19 ms
8,716 KB
testcase_19 AC 19 ms
8,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    W, H = map(int, input().split())
    C = [list(input()) for i in range(H)]
    col = 1

    for i in range(H):
        for j in range(W):
            if C[i][j] == '#':
                C[i][j] = 0
            elif C[i][j] == '.':
                bfs_color(W, H, C, i, j, col)
                col += 1
            else:
                pass


    cave1 = []
    cave2 = []

    for i in range(H):
        for j in range(W):
            if C[i][j] == 1:
                cave1.append((i, j))
            elif C[i][j] == 2:
                cave2.append((i, j))

    min_d = 1000

    for a in cave1:
        for b in cave2:
            min_d = min(min_d, dist(a, b) - 1)

    print(min_d)


def dist(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])


def bfs_color(W, H, C, i, j, col):
    nxt = deque([(i, j)])

    while nxt:
        y, x = nxt.pop()
        C[y][x] = col
        if x - 1 >= 0 and C[y][x-1] == '.':
            nxt.append((y, x-1))
        if x + 1 < W and C[y][x+1] == '.':
            nxt.append((y, x+1))
        if y - 1 >= 0 and C[y-1][x] == '.':
            nxt.append((y-1, x))
        if y + 1 < H and C[y+1][x] == '.':
            nxt.append((y+1, x))

    return None


if __name__ == '__main__':
    solve()
0