結果

問題 No.697 池の数はいくつか
ユーザー conf8o
提出日時 2020-02-07 14:49:20
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
MLE  
実行時間 -
コード長 1,031 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 511 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 583,128 KB
最終ジャッジ日時 2026-05-20 05:45:27
合計ジャッジ時間 17,667 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
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    dire = [(-1,0),(0,1),(1,0),(0,-1)]
    explored = set()
    ans = 0
    for h in range(H):
        for w in range(W):
            if A[h][w] == 0 or (h, w) in explored:
                continue
            ans += 1
            frontier = deque()
            frontier.append((h, w))
            explored.add((h, w))
            while frontier:
                y, x = frontier.popleft()
                for d, r in dire:
                    suc_y = y+d
                    suc_x = x+r
                    if suc_y >= H or suc_x >= W or \
                        suc_y < 0 or suc_x < 0 or \
                        (suc_y, suc_x) in explored or \
                        A[suc_y][suc_x] == 0:
                        continue

                    frontier.append((suc_y, suc_x))
                    explored.add((suc_y, suc_x))

    print(ans)

main()
0