結果

問題 No.2986 Permutation Puzzle
ユーザー ID 21712
提出日時 2025-02-23 00:20:28
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 2,583 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2025-02-23 00:20:33
合計ジャッジ時間 4,230 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 4
other WA * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

# this code generate by Grok3 (X.com)

import sys

# Read input
N, K = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
B = [list(map(int, input().split())) for _ in range(N)]

# Function to apply row permutation
def apply_row_perm(matrix, row_idx):
    perm = matrix[row_idx]
    new_matrix = [[] for _ in range(N)]
    for i in range(N):
        new_idx = perm.index(i + 1)
        new_matrix[new_idx] = matrix[i][:]
    return new_matrix

# Function to apply column permutation
def apply_col_perm(matrix, col_idx):
    perm = [matrix[i][col_idx] for i in range(N)]
    new_matrix = [[0] * N for _ in range(N)]
    for j in range(N):
        new_idx = perm.index(j + 1)
        for i in range(N):
            new_matrix[i][new_idx] = matrix[i][j]
    return new_matrix

# Check if two matrices are equal
def matrices_equal(m1, m2):
    return all(m1[i][j] == m2[i][j] for i in range(N) for j in range(N))

# Solve
operations = []
current = [row[:] for row in B]

# Step 1: Try to align rows
for i in range(N):
    target_row = A[i]
    for r in range(N):
        if current[r] == target_row:
            if r != i:
                # Use row r to permute rows
                operations.append(("R", r + 1))
                current = apply_row_perm(current, r)
            break
    else:
        # If no direct match, try to bring it closer (simplify by forcing a move)
        operations.append(("R", i + 1))
        current = apply_row_perm(current, i)

# Step 2: Align columns
for j in range(N):
    target_col = [A[i][j] for i in range(N)]
    current_col = [current[i][j] for i in range(N)]
    if target_col != current_col:
        for c in range(N):
            col_perm = [current[i][c] for i in range(N)]
            if col_perm == target_col:
                operations.append(("C", c + 1))
                current = apply_col_perm(current, c)
                break
        else:
            operations.append(("C", j + 1))
            current = apply_col_perm(current, j)

# Additional adjustments if not equal
max_attempts = 1000 - len(operations)
attempt = 0
while not matrices_equal(current, A) and attempt < max_attempts:
    # Try row or column operations alternately
    if attempt % 2 == 0:
        r = attempt % N
        operations.append(("R", r + 1))
        current = apply_row_perm(current, r)
    else:
        c = attempt % N
        operations.append(("C", c + 1))
        current = apply_col_perm(current, c)
    attempt += 1

# Output
print(len(operations))
for op_type, idx in operations:
    print(f"{op_type} {idx}")
0