結果

問題 No.697 池の数はいくつか
ユーザー はにはに
提出日時 2025-01-06 23:14:29
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 745 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 849,352 KB
最終ジャッジ日時 2025-01-06 23:15:44
合計ジャッジ時間 72,062 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other AC * 4 WA * 14 TLE * 8 MLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

h, w = map(int, input().split())
grid = [input().split() for _ in range(h)]

sensor = []
for j in range(h):
    for i in range(w):
        if grid[j][i] == '1':
            sensor.append((i, j))

visited = set()
count = 0

while sensor:
    q = deque()
    q.append(sensor[0])
    sensor.pop(0)
    
    while q:
        x, y = q.popleft()
        
        if (x, y) in visited:
           continue
        
        visited.add((x, y))
        
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < w and 0 <= ny < h:
               if grid[ny][nx] == '1' and (nx,ny) not in visited:
                  q.append((nx, ny))

    count += 1
print(count)
0