結果

問題 No.697 池の数はいくつか
ユーザー lllllll88938494
提出日時 2022-09-22 16:01:19
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
TLE  
実行時間 -
コード長 646 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 889 ms
コンパイル使用メモリ 20,696 KB
実行使用メモリ 15,356 KB
最終ジャッジ日時 2026-05-28 14:10:52
合計ジャッジ時間 14,360 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 TLE * 1 -- * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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