結果

問題 No.697 池の数はいくつか
ユーザー LyricalMaestro
提出日時 2024-12-05 23:50:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,601 ms / 6,000 ms
コード長 1,200 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 288,264 KB
最終ジャッジ日時 2024-12-05 23:50:48
合計ジャッジ時間 18,887 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/697

from collections import deque

DIRECTIONS = [(-1, 0), (1, 0), (0, 1), (0, -1)]

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

    
    composite_id_cell = [[-1] * W for _ in range(H)]
    composite_id = 0
    queue = deque()
    for s_h in range(H):
        for s_w in range(W):
            if A[s_h][s_w] == 1 and composite_id_cell[s_h][s_w] == -1:
                composite_id_cell[s_h][s_w] = composite_id
                queue.append((s_h, s_w))
                while len(queue) > 0:
                    h, w = queue.popleft()
                    for dh, dw in DIRECTIONS:
                        new_h = dh + h
                        new_w = dw + w
                        if 0 <= new_h < H and 0 <= new_w < W:
                            if A[new_h][new_w] == 1 and composite_id_cell[new_h][new_w] == -1:
                                composite_id_cell[new_h][new_w] = composite_id
                                queue.append((new_h, new_w))
                composite_id += 1

    print(composite_id)


                




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