結果

問題 No.697 池の数はいくつか
ユーザー dice4084
提出日時 2022-12-27 22:42:18
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 800 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 879,616 KB
最終ジャッジ日時 2024-11-22 09:49:04
合計ジャッジ時間 76,326 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample MLE * 3
other AC * 20 TLE * 7 MLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
from itertools import product

h, w = map(int, input().split())
a = [[0]*(w+2)] + [[0] + list(map(int, input().split())) + [0] for _ in range(h)] + [[0]*(w+2)]
arrived = [[False]*(w+2) for _ in range(h+2)]

dyx = ((1, 0), (-1, 0), (0, 1), (0, -1))
ans = 0
for i, j in product(range(1, h+1), range(1, w+1)):
    if a[i][j] == 0:
        continue
    if arrived[i][j]:
        continue

    ans += 1
    q = deque([(i, j)])
    while q:
        y, x = q.popleft()
        arrived[y][x] = True
        for dy, dx in dyx:
            new_y, new_x = (y+dy, x+dx)
            if a[new_y][new_x] == 0:
                continue
            if arrived[new_y][new_x]:
                continue
            q.append((new_y, new_x))

print(ans)
0