結果

問題 No.157 2つの空洞
ユーザー maspy
提出日時 2020-02-02 19:35:18
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 1,005 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 44,640 KB
最終ジャッジ日時 2024-09-18 21:21:45
合計ジャッジ時間 12,934 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

W,H = map(int,readline().split())
grid = np.frombuffer(read(),'S1').reshape(H,-1)[:,:W].astype('U1')

grid = np.ravel(grid).tolist()

component = [0] * (H*W)
n = 1
for i,x in enumerate(grid):
    if x != '.':
        continue
    if component[i]:
        continue
    stack = [i]
    component[i] = n
    while stack:
        x = stack.pop()
        for dx in [1,-1,W,-W]:
            y = x+dx
            if grid[y] != '.':
                continue
            if component[y]:
                continue
            component[y] = n
            stack.append(y)
    n += 1

component = np.array(component).reshape((H,W))

component

dist = np.where(component == 1, 0, 100)

for _ in range(4):
    dist = dist.T[::-1]
    n = dist.shape[0]
    for i in range(1,n):
        dist[i] = np.minimum(dist[i-1]+1, dist[i])

answer = dist[component == 2].min() - 1
print(answer)
0