結果

問題 No.2946 Puyo
ユーザー Theta
提出日時 2024-11-20 11:56:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,235 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 81,844 KB
実行使用メモリ 272,028 KB
最終ジャッジ日時 2024-11-20 11:56:40
合計ジャッジ時間 28,694 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import product
DIJ = ((0, 1), (0, -1), (1, 0), (-1, 0))


def main():
    H, W = map(int, input().split())
    board = [list(input()) for _ in range(H)]

    def is_valid_pos(h: int, w: int) -> bool:
        return 0 <= h < H and 0 <= w < W

    visited = set()
    for h, w in product(range(H), range(W)):
        if (h, w) in visited:
            continue
        current_con = set()
        q = [(h, w)]
        while q:
            cur = q.pop()
            if cur in current_con:
                continue
            current_con.add(cur)
            visited.add(cur)
            for dij in DIJ:
                next_ = (cur[0]+dij[0], cur[1]+dij[1])
                if next_ in visited:
                    continue
                if not is_valid_pos(*next_):
                    continue
                if board[cur[0]][cur[1]] != board[next_[0]][next_[1]]:
                    continue
                q.append(next_)
        if len(current_con) < 4:
            continue
        for mass in current_con:
            board[mass[0]][mass[1]] = "."
    for row in board:
        print("".join(row))


if __name__ == "__main__":
    main()
0