結果

問題 No.697 池の数はいくつか
ユーザー dango
提出日時 2023-06-14 19:19:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,159 ms / 6,000 ms
コード長 648 bytes
コンパイル時間 135 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 255,488 KB
最終ジャッジ日時 2024-06-23 02:31:28
合計ジャッジ時間 13,596 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
H, W = map(int,input().split())
L = [list(map(int,input().split())) for _ in range(H)]
def bfs(i, j):
    L[i][j] = 0
    Q = deque()
    Q.append((i, j))
    while Q:
        i, j = Q.popleft()
        dy = [0, 1, 0, -1]
        dx = [1, 0, -1, 0]
        for k in range(4):
            y = i + dy[k]
            x = j + dx[k]
            if not(0 <= y < H and 0 <= x < W):
                continue
            if L[y][x] == 1:
                L[y][x] = 0
                Q.append((y, x))
ans = 0
for i in range(H):
    for j in range(W):
        if L[i][j] == 1:
            ans += 1
            bfs(i, j)
print(ans)
0