結果

問題 No.697 池の数はいくつか
ユーザー dice4084
提出日時 2022-12-27 23:23:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,189 ms / 6,000 ms
コード長 837 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 287,432 KB
最終ジャッジ日時 2024-11-22 10:49:30
合計ジャッジ時間 22,185 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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)])
    arrived[i][j] = True
    while q:
        y, x = q.popleft()
        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))
            arrived[new_y][new_x] = True

print(ans)
0