結果

問題 No.697 池の数はいくつか
ユーザー lllllll88938494
提出日時 2022-09-22 15:59:49
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 646 bytes
コンパイル時間 583 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 852,968 KB
最終ジャッジ日時 2024-12-22 04:55:59
合計ジャッジ時間 76,851 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample MLE * 3
other AC * 18 TLE * 7 MLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

h,w=map(int,input().split())
grid=[list(map(int,input().split())) for i in range(h)]
f=[[0]*w for i in range(h)]
cnt = 1
l=[1,0,-1,0]
r=[0,1,0,-1]

def bfs(x,y):
    d=deque()
    d.append((x,y))

    while d:
        x,y=d.popleft()
        f[x][y] = cnt
        for i in range(4):
            if 0 <= x + l[i] < h and 0 <= y + r[i] < w:
                if grid[x+l[i]][y+r[i]] == 1 and f[x+l[i]][y+r[i]] == 0:
                    d.append((x+l[i],y+r[i]))


for i in range(h):
    for j in range(w):
        if grid[i][j] == 1 and f[i][j] ==0:
            bfs(i,j)
            cnt += 1
            
print(cnt-1) 
0