結果

問題 No.697 池の数はいくつか
ユーザー Theta
提出日時 2023-01-27 19:15:18
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
MLE  
実行時間 -
コード長 1,668 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 511,212 KB
最終ジャッジ日時 2024-06-28 04:36:47
合計ジャッジ時間 20,304 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 MLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from itertools import combinations, product


def main():
    H, W = map(int, input().split())
    field_map = [list(map(int, input().split())) for _ in range(H)]

    pond_ctr = 0
    searched = set()
    for height_idx, width_idx in product(range(H), range(W)):
        if field_map[height_idx][width_idx] == 0:
            continue
        if (height_idx, width_idx) in searched:
            continue

        pond_ctr += 1
        searching = deque(((height_idx, width_idx),))
        searched.add((height_idx, width_idx))
        while searching:
            current_h_idx, current_w_idx = searching.popleft()
            if field_map[current_h_idx][current_w_idx] == 0:
                continue

            if current_h_idx < H-1 and (current_h_idx+1, current_w_idx) not in searched:
                searching.append((current_h_idx+1, current_w_idx))
                searched.add((current_h_idx+1, current_w_idx))

            if current_h_idx > 0 and (current_h_idx-1, current_w_idx) not in searched:
                searching.append((current_h_idx-1, current_w_idx))
                searched.add((current_h_idx-1, current_w_idx))

            if current_w_idx < W-1 and (current_h_idx, current_w_idx+1) not in searched:
                searching.append((current_h_idx, current_w_idx+1))
                searched.add((current_h_idx, current_w_idx+1))

            if current_w_idx > 0 and (current_h_idx, current_w_idx-1) not in searched:
                searching.append((current_h_idx, current_w_idx-1))
                searched.add((current_h_idx, current_w_idx-1))

    print(pond_ctr)


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