結果

問題 No.460 裏表ちわーわ
ユーザー mkawa2mkawa2
提出日時 2020-04-29 13:44:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,992 bytes
コンパイル時間 1,551 ms
コンパイル使用メモリ 10,768 KB
実行使用メモリ 8,976 KB
最終ジャッジ日時 2023-08-18 20:55:02
合計ジャッジ時間 2,610 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,748 KB
testcase_01 AC 21 ms
8,792 KB
testcase_02 AC 21 ms
8,788 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 21 ms
8,956 KB
testcase_06 AC 22 ms
8,828 KB
testcase_07 AC 22 ms
8,884 KB
testcase_08 AC 22 ms
8,788 KB
testcase_09 AC 21 ms
8,884 KB
testcase_10 AC 22 ms
8,792 KB
testcase_11 AC 22 ms
8,900 KB
testcase_12 AC 22 ms
8,780 KB
testcase_13 AC 22 ms
8,832 KB
testcase_14 AC 22 ms
8,824 KB
testcase_15 AC 22 ms
8,956 KB
testcase_16 AC 22 ms
8,912 KB
testcase_17 AC 23 ms
8,884 KB
testcase_18 AC 23 ms
8,788 KB
testcase_19 AC 22 ms
8,780 KB
testcase_20 AC 22 ms
8,832 KB
testcase_21 AC 22 ms
8,824 KB
testcase_22 AC 22 ms
8,796 KB
testcase_23 AC 22 ms
8,936 KB
testcase_24 AC 22 ms
8,744 KB
testcase_25 RE -
testcase_26 AC 21 ms
8,904 KB
testcase_27 AC 21 ms
8,788 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
            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