結果

問題 No.697 池の数はいくつか
ユーザー vwxyz
提出日時 2022-01-05 08:01:51
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
MLE  
実行時間 -
コード長 678 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 574 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 378,076 KB
最終ジャッジ日時 2026-05-20 07:11:29
合計ジャッジ時間 23,311 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 MLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
import sys
readline=sys.stdin.readline


H,W=map(int,readline().split())
A=[list(map(int,readline().split())) for h in range(H)]
ans=0
seen=[[False]*W for h in range(H)]
for h in range(H):
    for w in range(W):
        if not seen[h][w] and A[h][w]:
            queue=[(h,w)]
            seen[h][w]=True
            ans+=1
            while queue:
                hh,ww=queue.pop()
                for dh,dw in ((0,1),(1,0),(0,-1),(-1,0)):
                    if 0<=hh+dh<H and 0<=ww+dw<W and A[hh+dh][ww+dw] and not seen[hh+dh][ww+dw]:
                        queue.append((hh+dh,ww+dw))
                        seen[hh+dh][ww+dw]=True
print(ans)
0