結果

問題 No.157 2つの空洞
ユーザー noko2250noko2250
提出日時 2015-03-20 20:51:52
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 11,028 KB
実行使用メモリ 8,672 KB
最終ジャッジ日時 2023-09-11 09:06:49
合計ジャッジ時間 1,916 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,404 KB
testcase_01 AC 16 ms
8,364 KB
testcase_02 AC 16 ms
8,272 KB
testcase_03 AC 16 ms
8,288 KB
testcase_04 AC 18 ms
8,348 KB
testcase_05 AC 19 ms
8,280 KB
testcase_06 AC 23 ms
8,380 KB
testcase_07 AC 17 ms
8,280 KB
testcase_08 AC 16 ms
8,344 KB
testcase_09 AC 19 ms
8,296 KB
testcase_10 AC 17 ms
8,340 KB
testcase_11 AC 27 ms
8,436 KB
testcase_12 AC 41 ms
8,444 KB
testcase_13 AC 51 ms
8,540 KB
testcase_14 AC 56 ms
8,364 KB
testcase_15 AC 77 ms
8,480 KB
testcase_16 AC 104 ms
8,504 KB
testcase_17 AC 48 ms
8,552 KB
testcase_18 AC 94 ms
8,624 KB
testcase_19 AC 72 ms
8,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/c/Python34/python
# coding: utf-8
inf = 10**9


def dfs(y, x, val, H, W, C, D):
    for dx, dy in zip([0, 1, 0, -1], [-1, 0, 1, 0]):
        nx, ny = x+dx, y+dy
        if 0 <= nx < W and 0 <= ny < H:
            if C[ny][nx] == '.':
                if D[ny][nx] <= val:
                    continue
                D[ny][nx] = val
            else:
                if D[ny][nx] <= val+1:
                    continue
                D[ny][nx] = val + 1
            dfs(ny, nx, D[ny][nx], H, W, C, D)


def startPoint(H, W, C):
    for y in range(H):
        for x in range(W):
            if C[y][x] == '.':
                return x, y


def main():
    [W, H] = map(int, input().split())
    C = [input() for _ in range(H)]
    D = [[inf] * W for _ in range(H)]

    sx, sy = startPoint(H, W, C)
    D[sy][sx] = 0
    dfs(sy, sx, 0, H, W, C, D)
    ans = inf
    for y in range(H):
        for x in range(W):
            if C[y][x] == '.' and D[y][x] > 0:
                ans = min(ans, D[y][x])
    print(ans)
    return

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