結果

問題 No.460 裏表ちわーわ
ユーザー mkawa2mkawa2
提出日時 2020-04-29 13:52:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,131 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-08-18 22:14:57
合計ジャッジ時間 2,600 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,752 KB
testcase_01 AC 19 ms
8,748 KB
testcase_02 AC 20 ms
8,628 KB
testcase_03 AC 19 ms
8,564 KB
testcase_04 AC 20 ms
8,608 KB
testcase_05 AC 20 ms
8,732 KB
testcase_06 AC 20 ms
8,580 KB
testcase_07 AC 20 ms
8,580 KB
testcase_08 AC 20 ms
8,624 KB
testcase_09 AC 20 ms
8,556 KB
testcase_10 AC 20 ms
8,716 KB
testcase_11 AC 21 ms
8,604 KB
testcase_12 AC 21 ms
8,616 KB
testcase_13 AC 21 ms
8,564 KB
testcase_14 AC 21 ms
8,616 KB
testcase_15 AC 20 ms
8,696 KB
testcase_16 AC 20 ms
8,732 KB
testcase_17 AC 20 ms
8,576 KB
testcase_18 AC 20 ms
8,704 KB
testcase_19 AC 20 ms
8,556 KB
testcase_20 AC 20 ms
8,612 KB
testcase_21 AC 20 ms
8,576 KB
testcase_22 AC 20 ms
8,628 KB
testcase_23 AC 20 ms
8,716 KB
testcase_24 AC 20 ms
8,616 KB
testcase_25 AC 19 ms
8,700 KB
testcase_26 AC 19 ms
8,580 KB
testcase_27 AC 19 ms
8,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

# ライツアウトっていうらしい
# 連立方程式作って掃き出し法で解く方法もあるらしい
def main():
    inf = 10 ** 9
    h, w = MI()
    # 各行を2進数として入力する
    aa0 = [int("".join(SI().split()), 2) for _ in range(h)]
    # print(*[format(a,"b").zfill(w) for a in aa])

    # 前計算として、行の状態bitを作るために必要な最小手数cnt[bit]をbfsで作る
    # cnt[bit]==infの場合は作れないということ
    cnt = [inf] * (1 << w)
    q = deque()
    q.append((0, 0))
    cnt[0] = 0
    mask = (1 << w) - 1
    while q:
        c, bit = q.popleft()
        for j in range(w):
            # 7=111(2),3=11(2)
            if j: nbit = (bit ^ 7 << (j - 1)) & mask
            else: nbit = (bit ^ 3) & mask
            if cnt[nbit] != inf: continue
            cnt[nbit] = c + 1
            q.append((c + 1, nbit))

    # 最初の行の選び方はすべて試す
    # 次の行からは自動的に決まるので、上から0が埋まっていくように決めていく
    ans = inf
    for bit, c in enumerate(cnt):
        flag = False
        if c == inf: continue
        aa = aa0[:]
        # 最初の行に対する操作の結果
        for i in range(min(2, h)): aa[i] ^= bit
        # 次の行以降
        for i in range(1, h):
            bit = aa[i - 1]
            if cnt[bit] == inf:
                flag = True
                break
            c += cnt[bit]
            for ni in range(i, min(i + 2, h)):
                aa[ni] ^= bit
        if flag: continue
        # 最終行が0だけになったら成功
        if aa[h - 1] == 0: ans = min(ans, c)
    if ans == inf: print("Impossible")
    else: print(ans)

main()
0