結果

問題 No.697 池の数はいくつか
ユーザー LyricalMaestro
提出日時 2024-12-05 23:50:26
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,200 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 333 ms
コンパイル使用メモリ 85,132 KB
実行使用メモリ 306,648 KB
最終ジャッジ日時 2026-05-24 16:44:20
合計ジャッジ時間 15,354 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## 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