結果

問題 No.697 池の数はいくつか
ユーザー neko_the_shadow
提出日時 2018-06-09 22:18:16
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 603 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 82,792 KB
実行使用メモリ 849,440 KB
最終ジャッジ日時 2024-11-25 13:04:04
合計ジャッジ時間 19,791 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools

if __name__ == '__main__':
    h, w = map(int, input().split())
    matrix = [list(map(int, input().split())) for _ in range(h)]
    
    diffs = ((1, 0), (-1, 0), (0, 1), (0, -1))
    counter = 0
    for x, y in itertools.product(range(h), range(w)):
        if matrix[x][y] == 0: continue
        
        counter += 1
        stack = [(x, y)]
        while stack:
            i, j = stack.pop()
            if 0 <= i < h and 0 <= j < w and matrix[i][j] == 1:
                matrix[i][j] = 0
                stack.extend((i + di, j + dj) for di, dj in diffs)
    
    print(counter)
0