結果

問題 No.460 裏表ちわーわ
ユーザー mkawa2mkawa2
提出日時 2020-04-29 13:46:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,047 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 10,996 KB
実行使用メモリ 9,008 KB
最終ジャッジ日時 2023-08-18 21:00:01
合計ジャッジ時間 2,254 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,820 KB
testcase_01 AC 21 ms
8,984 KB
testcase_02 AC 21 ms
8,820 KB
testcase_03 AC 22 ms
8,924 KB
testcase_04 AC 21 ms
8,880 KB
testcase_05 AC 23 ms
9,008 KB
testcase_06 AC 23 ms
8,832 KB
testcase_07 AC 23 ms
8,928 KB
testcase_08 AC 24 ms
8,812 KB
testcase_09 AC 22 ms
9,000 KB
testcase_10 AC 24 ms
8,812 KB
testcase_11 AC 22 ms
8,820 KB
testcase_12 AC 22 ms
8,988 KB
testcase_13 AC 23 ms
8,836 KB
testcase_14 AC 23 ms
8,940 KB
testcase_15 AC 23 ms
8,796 KB
testcase_16 AC 23 ms
8,876 KB
testcase_17 AC 23 ms
8,972 KB
testcase_18 AC 23 ms
8,968 KB
testcase_19 AC 23 ms
8,980 KB
testcase_20 AC 22 ms
8,976 KB
testcase_21 AC 23 ms
8,980 KB
testcase_22 AC 23 ms
8,956 KB
testcase_23 AC 24 ms
8,812 KB
testcase_24 AC 23 ms
8,808 KB
testcase_25 AC 22 ms
8,960 KB
testcase_26 AC 23 ms
8,792 KB
testcase_27 AC 22 ms
8,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from copy import deepcopy
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 = deepcopy(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