結果
| 問題 |
No.2986 Permutation Puzzle
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-12-25 19:02:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 23 ms / 2,000 ms |
| コード長 | 5,232 bytes |
| コンパイル時間 | 1,409 ms |
| コンパイル使用メモリ | 122,532 KB |
| 最終ジャッジ日時 | 2025-02-26 16:46:33 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:149:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::vector<std::pair<char, int> >::size_type’ {aka ‘long unsigned int’} [-Wformat=]
149 | printf("%d\n", res.size());
| ~^ ~~~~~~~~~~
| | |
| int std::vector<std::pair<char, int> >::size_type {aka long unsigned int}
| %ld
main.cpp: In function ‘int main()’:
main.cpp:157:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
157 | scanf("%d", b[i] + j);
| ~~~~~^~~~~~~~~~~~~~~~
main.cpp:160:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
160 | scanf("%d", a[i] + j), t[i][j] = b[i][j];
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <ctime>
#include <assert.h>
#include <deque>
#include <list>
#include <stack>
using namespace std;
#define is_mul_overflow(a, b) \
((b != 0) && (a > LLONG_MAX / b || a < LLONG_MIN / b))
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<long long , long long> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<int, long long > pil;
typedef pair<long long, pii> plii;
typedef pair<double, int> pdi;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ull, ull> puu;
typedef long double ld;
const int N = 2000086, MOD = 998244353, INF = 0x3f3f3f3f, MID = 333;
const long double EPS = 1e-8;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
// int dx[8] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int n, m, cnt;
int w[N];
vector<ll> num;
ll res;
ll lowbit(ll x) { return x & -x; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
inline double rand(double l, double r) { return (double)rand() / RAND_MAX * (r - l) + l; }
inline ll qmi(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1) res = res * a % c; a = a * a % c; b >>= 1; } return res; }
inline ll qmi(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; }
inline double qmi(double a, ll b) { double res = 1; while (b) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; }
// inline ll C(ll a, ll b) { if (a < b) return 0; if (b > a - b) b = a - b; ll res = 1; for (ll i = 1, j = a; i <= b; i++, j--) { res = res * (j % MOD) % MOD; res = res * qmi(i, MOD - 2, MOD) % MOD; } return res; }
inline ll C(ll a, ll b, int* c) { if (a < b) return 0; ll res = 1; for (ll j = a, i = 1; i < b + 1; i++, j--) res *= j; for (ll j = a, i = 1; i < b + 1; i++, j--) res /= i; return res; }
inline int find_(int x) { return lower_bound(num.begin(), num.end(), x) - num.begin(); }
int a[11][11], b[11][11], t[11][11], tar[6][11][11];
vector<pair<char, int> > ans;
bool check(int a[11][11], int b[11][11]) {
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
if (a[i][j] != b[i][j]) return 0;
return 1;
}
bool dfs(int t[11][11], int c) {
if (c == m) {
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
if (t[i][j] != a[i][j]) return 0;
return 1;
}
int tmp[11][11], t2[11][11];
for (int p = 1; p < n + 1; p++) {
ans.push_back({'R', t[p][p]});
memcpy(tmp, t, sizeof tmp);
memcpy(t2, tmp, sizeof t2);
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
tmp[t2[p][i]][j] = t2[i][j];
if (dfs(tmp, c + 1)) {
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
tar[c + 1][i][j] = tmp[i][j];
return 1;
}
ans.pop_back();
}
for (int p = 1; p < n + 1; p++) {
ans.push_back({'C', t[p][p]});
memcpy(tmp, t, sizeof tmp);
memcpy(t2, tmp, sizeof t2);
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
tmp[i][t2[j][p]] = t2[i][j];
if (dfs(tmp, c + 1)) {
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
tar[c + 1][i][j] = tmp[i][j];
return 1;
}
ans.pop_back();
}
return 0;
}
inline void solve() {
dfs(t, 0);
vector<pair<char, int> > res;
memcpy(tar[0], b, sizeof tar[0]);
for (int i = m - 1; i > -1; i--) {
auto u = ans.back();
ans.pop_back();
if (u.first == 'R') {
int ne = u.second;
while (!check(tar[i], a)) {
res.push_back({'R', ne});
memcpy(t, a, sizeof t);
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
a[t[ne][i]][j] = t[i][j];
ne = t[ne][ne];
}
} else {
int ne = u.second;
while (!check(tar[i], a)) {
res.push_back({'C', ne});
memcpy(t, a, sizeof t);
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
a[i][t[j][ne]] = t[i][j];
ne = t[ne][ne];
}
}
}
printf("%d\n", res.size());
for (auto u : res) printf("%c %d\n", u.first, u.second);
}
int main() {
cin >> n >> m;
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
scanf("%d", b[i] + j);
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++)
scanf("%d", a[i] + j), t[i][j] = b[i][j];
solve();
return 0;
}