結果

問題 No.697 池の数はいくつか
ユーザー neko_the_shadow
提出日時 2018-06-09 22:30:53
言語 Python3
(3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 673 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 681 ms
コンパイル使用メモリ 14,976 KB
実行使用メモリ 87,936 KB
最終ジャッジ日時 2026-09-29 21:27:05
合計ジャッジ時間 17,155 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 9 WA * 17 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import itertools

if __name__ == '__main__':
    h, w = map(int, input().split())
    matrix = [list(map(int, input().split())) for _ in range(h)]
    
    diffs = ((1, 0), (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 not(0 <= i < h and 0 <= j < w and matrix[i][j] == 1): continue

            matrix[i][j] = 0
            for di, dj in diffs:
                ni, nj = i + di, j + dj
                if 0 <= ni < h and 0 <= nj < w: stack.append((ni, nj))
    
    print(counter)
0