結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-03-23 16:07:05 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 745 bytes | 
| コンパイル時間 | 287 ms | 
| コンパイル使用メモリ | 82,068 KB | 
| 実行使用メモリ | 519,676 KB | 
| 最終ジャッジ日時 | 2024-10-11 19:04:11 | 
| 合計ジャッジ時間 | 22,937 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 MLE * 2 | 
ソースコード
h, w = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(h)]
used = [[False] * w for _ in range(h)]
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
ans = 0
for i in range(h):
    for j in range(w):
        if used[i][j] or A[i][j] == 0:
            continue
        ans += 1
        stack = [(i, j)]
        while stack:
            ii, jj = stack.pop()
            for di, dj in directions:
                ni = ii + di
                nj = jj + dj
                if ni == -1 or ni == h or nj == -1 or nj == w:
                    continue
                if A[ni][nj] == 0 or used[ni][nj]:
                    continue
                used[ni][nj] = True
                stack.append((ni, nj))
        
print(ans)