結果
| 問題 |
No.2986 Permutation Puzzle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-12-11 21:27:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 2,000 ms |
| コード長 | 2,939 bytes |
| コンパイル時間 | 2,586 ms |
| コンパイル使用メモリ | 209,016 KB |
| 最終ジャッジ日時 | 2025-02-26 12:01:52 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using T = array<array<int,10>,10>;
int n;
void dump(T B){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cerr << B[i][j] + 1 << (j + 1 == n ? '\n' : ' ');
}
}
}
T gen(T &A, char c, int p){
T B{{}};
array<int, 10> tr{};
if(c == 'R'){
for(int i = 0; i < n; i++) tr[i] = A[p][i];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
B[tr[i]][j] = A[i][j];
}
}
}else{
for(int i = 0; i < n; i++) tr[i] = A[i][p];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
B[i][tr[j]] = A[i][j];
}
}
}
return B;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int k;
cin >> n >> k;
T A{{}}, B{{}};
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> A[i][j];
A[i][j]--;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> B[i][j];
B[i][j]--;
}
}
vector<pair<char,int>> dir;
for(int i = 0; i < n; i++){
dir.emplace_back('R', i);
dir.emplace_back('C', i);
}
sort(dir.begin(), dir.end());
vector<T> stk;
vector<char> ope;
auto dfs = [&](auto dfs, int i) -> bool {
if (stk.back() == B) return true;
if(i == k) return false;
for(auto [c, p] : dir){
stk.emplace_back(gen(stk.back(), c, p));
ope.emplace_back(c);
if(dfs(dfs, i + 1)) return true;
ope.pop_back();
stk.pop_back();
}
return false;
};
auto search = [&](char c, int v, T& tmp){
if(c == 'C'){
for(int j = 0; j < n; j++){
if(tmp[0][j] == v) return j;
}
}
for(int j = 0; j < n; j++){
if(tmp[j][0] == v) return j;
}
return -1;
};
stk.emplace_back(A);
dfs(dfs, 0);
reverse(stk.begin(), stk.end());
reverse(ope.begin(), ope.end());
vector<pair<char,int>> ans;
for(int i = 0; i + 1 < stk.size(); i++){
char c = ope[i];
for(int j = 0; j < n; j++){
T S = stk[i];
int cnt = -1;
for(int k = 1; k <= 250; k++){
S = gen(S, c, search(c, j, S));
if(S == stk[i + 1]){
cnt = k;
break;
}
}
if(cnt != -1){
S = stk[i];
while(cnt--){
int p = search(c, j, S);
ans.emplace_back(c, p);
S = gen(S, c, p);
}
break;
}
}
}
cout << ans.size() << '\n';
for(auto [c, p] : ans) cout << c << ' ' << p + 1 << '\n';
}