結果

問題 No.697 池の数はいくつか
ユーザー H3PO4
提出日時 2020-12-06 22:07:11
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,006 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 836,728 KB
最終ジャッジ日時 2024-11-25 16:44:48
合計ジャッジ時間 59,585 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 TLE * 2 MLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
from sys import stdin
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix


def main():
    H, W = map(int, stdin.buffer.readline().split())
    A = (np.frombuffer(stdin.buffer.read(), dtype='S1')[::2] == b'1').reshape((H, W))
    F = np.arange(H * W, dtype='u4').reshape((H, W))
    ct_0 = np.count_nonzero(~A)

    HC = A[1:] & A[:-1]
    ZW = np.zeros(W, dtype=bool).reshape(1, W)
    frm1 = F[np.concatenate([ZW, HC], 0)]
    to1 = F[np.concatenate([HC, ZW], 0)]
    del HC, ZW

    WC = A[:, 1:] & A[:, :-1]
    del A
    ZH = np.zeros(H, dtype=bool).reshape(H, 1)
    frm2 = F[np.concatenate([ZH, WC], 1)]
    to2 = F[np.concatenate([WC, ZH], 1)]
    del WC, ZH
    del F

    frm = np.hstack((frm1, frm2))
    to = np.hstack((to1, to2))
    L = len(frm)
    matr = csr_matrix(([1] * L, (frm, to)), shape=(H * W, H * W))
    del frm, to
    p = connected_components(matr, return_labels=False)
    ans = p - ct_0
    return ans


print(main())
0