結果
| 問題 |
No.2986 Permutation Puzzle
|
| コンテスト | |
| ユーザー |
ID 21712
|
| 提出日時 | 2025-04-17 15:58:58 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,583 bytes |
| コンパイル時間 | 759 ms |
| コンパイル使用メモリ | 12,544 KB |
| 実行使用メモリ | 17,920 KB |
| 最終ジャッジ日時 | 2025-04-17 15:59:09 |
| 合計ジャッジ時間 | 9,959 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 RE * 3 |
| other | RE * 40 |
ソースコード
# this code was generated by grok 3 beta (x.com)
def inverse_permutation(perm):
# 順列の逆順列を計算(1-based)
n = len(perm)
inv = [0] * n
for i in range(n):
inv[perm[i] - 1] = i + 1
return inv
def apply_row_perm(matrix, row_idx, perm):
# 行を順列に基づいて並び替える(行のインデックスを変更)
n = len(matrix)
new_matrix = [None] * n
for i in range(n):
new_matrix[i] = matrix[perm[i] - 1][:] # 行 perm[i] を i 番目に
return new_matrix
def apply_col_perm(matrix, col_idx, perm):
# 列を順列に基づいて並び替える(列のインデックスを変更)
n = len(matrix)
new_matrix = [[0] * n for _ in range(n)]
for j in range(n):
for i in range(n):
new_matrix[i][j] = matrix[i][perm[j] - 1] # 列 perm[j] を j 番目に
return new_matrix
def check_equal(matrix1, matrix2):
# 2つの行列が等しいか確認
return all(row1 == row2 for row1, row2 in zip(matrix1, matrix2))
def dfs(current, A, operations, limit, N):
# バックトラックで B から A への操作列を探索
if check_equal(current, A):
return operations
if len(operations) >= limit:
return None
# 各行を試す
for i in range(N):
perm = current[i]
inv_perm = inverse_permutation(perm)
new_matrix = apply_row_perm(current, i, inv_perm)
new_ops = operations + [("R", i + 1)]
result = dfs(new_matrix, A, new_ops, limit, N)
if result is not None:
return result
# 各列を試す
for j in range(N):
perm = [current[i][j] for i in range(N)]
inv_perm = inverse_permutation(perm)
new_matrix = apply_col_perm(current, j, inv_perm)
new_ops = operations + [("C", j + 1)]
result = dfs(new_matrix, A, new_ops, limit, N)
if result is not None:
return result
return None
def solve(N, K, A, B):
# B から A への操作列をバックトラックで探索
operations = []
limit = 1000 # 操作回数の上限
result = dfs(B, A, operations, limit, N)
if result is None:
return [] # 解が見つからない場合(問題保証により発生しない)
return result
# 入力処理
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)]
# 解を求める
ops = solve(N, K, A, B)
# 出力
print(len(ops))
for op_type, pos in ops:
print(f"{op_type} {pos}")
ID 21712