結果

問題 No.697 池の数はいくつか
ユーザー MShinya
提出日時 2019-05-20 13:56:01
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,004 ms / 6,000 ms
コード長 695 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,112 ms
コンパイル使用メモリ 85,120 KB
実行使用メモリ 248,704 KB
最終ジャッジ日時 2026-05-08 14:21:11
合計ジャッジ時間 9,029 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
ans = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == 1:
            ans += 1
            que = deque()
            que.append((h, w))
            A[h][w] = 0
            while que:
                y, x = que.popleft()
                for i in range(4):
                    xx = x + dx[i]
                    yy = y + dy[i]
                    if 0 <= xx < W and 0 <= yy < H:
                        if A[yy][xx] == 1:
                            que.append((yy, xx))
                            A[yy][xx] = 0
print(ans)
0