結果

問題 No.697 池の数はいくつか
ユーザー DrDrpilot
提出日時 2022-01-21 16:03:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,723 ms / 6,000 ms
コード長 596 bytes
コンパイル時間 428 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 254,720 KB
最終ジャッジ日時 2024-11-08 09:01:16
合計ジャッジ時間 16,233 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
h,w=map(int,input().split())
g=[list(map(int,input().split())) for _ in range(h)]
def bfs(i,j):
    g[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 g[y][x]==1:
                g[y][x]=0
                q.append((y,x))
ans=0
for i in range(h):
    for j in range(w):
        if g[i][j]==1:
            ans+=1
            bfs(i,j)
print(ans)
0