結果

問題 No.697 池の数はいくつか
ユーザー はにはに
提出日時 2025-01-06 23:13:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
MLE  
実行時間 -
コード長 875 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 817,632 KB
最終ジャッジ日時 2025-01-06 23:14:17
合計ジャッジ時間 51,825 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 MLE * 2
other AC * 25 TLE * 3 MLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
h, w = map(int, input().split())
grid = [list(input().split()) for _ in range(h)]

sensor = set()
q = deque()
visited = [[False for _ in range(w)] for _ in range(h)]
for j in range(h):
    for i in range(w):
        if grid[j][i] == '1':
            sensor.add((i, j))
count = 0
while sensor:
    q.append(sensor.pop())
    while q:
        x, y = q.popleft()
        if (x, y) in sensor:
            sensor.remove((x, y))
        
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nx, ny = x + dx, y + dy
            if nx < 0 or w <= nx or ny < 0 or h <= ny:
                continue
            if grid[ny][nx] == '0':
                continue
            if not visited[ny][nx] and grid[ny][nx] == '1':
                visited[ny][nx] = True
                q.append((nx, ny))
                
    count += 1
print(count)
0