結果

問題 No.460 裏表ちわーわ
ユーザー maspy
提出日時 2020-03-12 20:27:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 1,363 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,476 KB
最終ジャッジ日時 2024-11-19 06:38:03
合計ジャッジ時間 7,558 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3.8
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools

INF = 1000

# %%
M, N, *A = map(int, read().split())

# %%
B = [0] * (N + 2) * (M + 2)
for i in range(1, M + 1):
    B[(N + 2) * i + 1: (N + 2) * i + N + 1] = A[N * i - N: N * i]


# %%
def test(first_row, first_col):
    dxs = tuple((N + 2) * x + y for x in (-1, 0, 1) for y in (-1, 0, 1))
    C = B[:]
    counter = 0

    def push_button(i):
        nonlocal counter
        counter += 1
        for dx in dxs:
            C[i + dx] ^= 1
    for i, x in enumerate(first_row):
        if x:
            push_button(N + 3 + i)
    for j, x in enumerate(first_col, 2):
        if x:
            push_button((N + 2) * j + 1)
    for i in range(1, M):
        for j in range(1, N):
            if C[(N + 2) * i + j]:
                push_button((N + 2) * (i + 1) + (j + 1))
    if any(C[M * (N + 2) + j] for j in range(1, N + 1)):
        return INF
    if any(C[i * (N + 2) + N] for i in range(1, M + 1)):
        return INF
    return counter


# %%
row_patterns = itertools.product([0, 1], repeat=N)
col_patterns = itertools.product([0, 1], repeat=(M - 1))
answer = min(test(r, c) for r, c in itertools.product(row_patterns, col_patterns))
if answer == INF:
    answer = 'Impossible'
print(answer)
0