結果

問題 No.585 工夫のないパズル
ユーザー maspymaspy
提出日時 2020-05-03 18:49:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,282 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 11,060 KB
実行使用メモリ 8,372 KB
最終ジャッジ日時 2023-09-04 05:07:33
合計ジャッジ時間 1,813 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,160 KB
testcase_01 AC 17 ms
8,224 KB
testcase_02 AC 17 ms
8,236 KB
testcase_03 AC 17 ms
8,236 KB
testcase_04 AC 17 ms
8,196 KB
testcase_05 AC 16 ms
8,236 KB
testcase_06 AC 17 ms
8,360 KB
testcase_07 AC 17 ms
8,240 KB
testcase_08 AC 17 ms
8,372 KB
testcase_09 AC 17 ms
8,168 KB
testcase_10 AC 17 ms
8,292 KB
testcase_11 AC 17 ms
8,328 KB
testcase_12 AC 17 ms
8,300 KB
testcase_13 AC 17 ms
8,324 KB
testcase_14 AC 17 ms
8,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

class Grid:
    def __init__(self, S):
        self.S = list(S)
        self.cnt = 0
        self.log = []

    def __repr__(self):
        return '\n'.join(''.join(self.S[4 * i:4 * i + 4]) for i in range(4))

    def __getitem__(self, a):
        n = self.S.index(a)
        return divmod(n, 4)

    def shift_row(self, i, k):
        k %= 4
        if k:
            self.log.append(f'R {i} {k}')
        A = self.S[4 * i:4 * i + 4]
        self.S[4 * i:4 * i + 4] = A[-k:] + A[:-k]

    def shift_col(self, i, k):
        k %= 4
        if k:
            self.log.append(f'C {i} {k}')
        A = self.S[i::4]
        self.S[i::4] = A[-k:] + A[:-k]

def move(G, a, n):
    """ n-1 以下のセルを固定したまま a を n に運ぶ """
    r, c = divmod(n, 4)
    r1, c1 = G[a]
    if r == r1:
        # 一度同じ行から出す
        G.shift_col(c1, 1)
        k = 1 if (c1 + 1 - c) % 4 != 0 else 2
        G.shift_row(r1 + 1, k)
        G.shift_col(c1, -1)
    r1, c1 = G[a]
    # 同じ列にあればよける
    if c == c1:
        G.shift_row(r1, 1)
    # 収納
    r1, c1 = G[a]
    G.shift_col(c, r1 - r)
    G.shift_row(r1, (c - c1) % 4)
    G.shift_col(c, r - r1)

def final_row(G):
    r, c = G['M']
    G.shift_row(3, -c)
    A = ''.join(G.S[13:])

    def fundamental_pattern(G, i, k):
        # 7 手で、最終行に偶置換
        G.shift_col(i, 1)
        G.shift_row(3, k)
        G.shift_col(i, -1)
        G.shift_row(3, k + k)
        G.shift_col(i, 1)
        G.shift_row(3, k)
        G.shift_col(i, -1)

    if A == 'NOP':
        pass
    elif A == 'NPO':
        G.shift_row(3, 1)
        fundamental_pattern(G, 1, 1)
    elif A == 'ONP':
        G.shift_row(3, 1)
        fundamental_pattern(G, 0, 1)
    elif A == 'OPN':
        fundamental_pattern(G, 2, 3)
    elif A == 'PNO':
        fundamental_pattern(G, 2, 1)
    else:
        fundamental_pattern(G, 0, 1)
        G.shift_row(3, 1)
        fundamental_pattern(G, 2, 1)
        return

S = ''.join(read().decode().split())

G = Grid(S)
for n in range(12):
    a = chr(ord('A') + n)
    move(G, a, n)
final_row(G)

log = G.log
print(len(log))
print('\n'.join(log))
0