結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-02-07 18:34:01 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 874 bytes | 
| コンパイル時間 | 202 ms | 
| コンパイル使用メモリ | 12,416 KB | 
| 実行使用メモリ | 817,784 KB | 
| 最終ジャッジ日時 | 2024-11-25 15:52:01 | 
| 合計ジャッジ時間 | 43,204 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | MLE * 3 | 
| other | AC * 26 TLE * 3 MLE * 3 | 
ソースコード
from collections import deque
import sys
input = sys.stdin.readline
def readlines(n):
	for h in range(n):
		for w, bit in enumerate(input().split()):
			if bit == "1":
				yield h,w
	
def main():
    H, W = map(int, input().split())
    A = set(readlines(H))
    dire = [(-1,0),(0,1),(1,0),(0,-1)]
    size = len(A)
    ans = 0
    for h,w in list(A):
        if size < 0:
            break
        if (h,w) not in A:
            continue
        ans += 1
        frontier = deque()
        frontier.append((h, w))
        while frontier:
            y, x = frontier.popleft()
            for d, r in dire:
                suc_y = y+d
                suc_x = x+r
                if (suc_y, suc_x) not in A:
                    continue
                frontier.append((suc_y, suc_x))
                A.remove((suc_y,suc_x))
                size -= 1
    print(ans)
main()