結果

問題 No.697 池の数はいくつか
ユーザー rlangevin
提出日時 2023-01-26 20:36:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,214 ms / 6,000 ms
コード長 857 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 81,992 KB
実行使用メモリ 281,472 KB
最終ジャッジ日時 2024-06-27 12:00:06
合計ジャッジ時間 15,226 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *

H, W = map(int, input().split())
G = []
for i in range(H):
    G.append(list(map(int, input().split())))
    
seen = [0] * (H * W)
ans = 0
Q = deque()
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(H):
    for j in range(W):
        if not G[i][j]:
            continue
        if seen[i*W+j]:
            continue
        seen[i*W+j] = 1
        ans += 1
        Q.append(i*W+j)
        while Q:
            px, py = divmod(Q.popleft(), W)
            for k in range(4):
                x = px + dx[k]
                y = py + dy[k]
                if x < 0 or x > H - 1 or y < 0 or y > W - 1:
                    continue
                if seen[x*W+y]:
                    continue
                if not G[x][y]:
                    continue
                seen[x*W+y] = 1
                Q.append(x * W + y)
print(ans)
0