結果
| 問題 |
No.3131 Twin Slide Puzzles
|
| コンテスト | |
| ユーザー |
Yudai0606Nazo
|
| 提出日時 | 2025-07-31 12:25:25 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 377 ms / 4,000 ms |
| コード長 | 3,477 bytes |
| コンパイル時間 | 4,042 ms |
| コンパイル使用メモリ | 266,140 KB |
| 実行使用メモリ | 26,112 KB |
| 最終ジャッジ日時 | 2025-07-31 12:25:48 |
| 合計ジャッジ時間 | 22,517 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 59 |
コンパイルメッセージ
main.cpp: In function ‘bool can_solve(std::vector<int>&, int)’:
main.cpp:34:49: warning: ‘pi’ may be used uninitialized [-Wmaybe-uninitialized]
34 | return ((n - uf.groups().size())&1) == ((pi + pj)&1);
| ~~~~^~~~~
main.cpp:30:9: note: ‘pi’ was declared here
30 | int pi, pj;
| ^~
main.cpp:34:49: warning: ‘pj’ may be used uninitialized [-Wmaybe-uninitialized]
34 | return ((n - uf.groups().size())&1) == ((pi + pj)&1);
| ~~~~^~~~~
main.cpp:30:13: note: ‘pj’ was declared here
30 | int pi, pj;
| ^~
ソースコード
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <cassert>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
//using mint = modint1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--)
#define all(v) v.begin(), v.end()
void _u() { cerr << endl; } template <class H, class... T> void _u(H&& h, T&&... t) { cerr << h << ", "; _u(move(t)...); }
#define U(...) { cerr << #__VA_ARGS__ << ": "; _u(__VA_ARGS__); }
template<typename T> bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; }
template<typename T> bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; }
template<typename T> istream& operator>>(istream &in, vector<T> &a) { for(T &x: a) in >> x; return in; }
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for(const T &x: a) out << x << ' '; return out; }
const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
bool can_solve(vector<int> &v, int m) {
int n = v.size();
dsu uf(n);
rep(i, n) uf.merge(i, v[i]);
int pi, pj;
rep(i, n) if(v[i] == 0) {
pi = i / m, pj = i % m;
}
return ((n - uf.groups().size())&1) == ((pi + pj)&1);
}
ll calc(vector<int> &v, vector<vector<int>> &a) {
int n = v.size(), m = a.size();
ll res = 0;
rep(i, n) {
int pi = i / m, pj = i % m;
res += ll(a[pi][pj]) * v[i];
}
return res;
}
int main() {
int n;
cin >> n;
vector<vector<int>> a(n, vector<int>(n));
cin >> a;
if(n <= 3) {
vector<int> p(n*n);
iota(all(p), 0);
map<ll, vector<int>> res;
do {
if(!can_solve(p, n)) continue;
auto score = calc(p, a);
if(res.count(score)) {
cout << "Yes" << endl;
auto q = res[score];
rep(i, n) {
rep(j, n) cout << p[i*n+j] << " ";
cout << '\n';
}
rep(i, n) {
rep(j, n) cout << q[i*n+j] << " ";
cout << '\n';
}
return 0;
}
res[score] = p;
} while(next_permutation(all(p)));
cout << "No" << endl;
} else {
cout << "Yes" << endl;
vector<int> p(14);
iota(all(p), 0);
map<ll, vector<int>> res;
random_device rd;
mt19937 rng(rd());
while(1) {
shuffle(all(p), rng);
if(!can_solve(p, n)) continue;
ll score = calc(p, a);
if(res.count(score) && res[score] != p) {
auto q = res[score];
rep(i, n) {
rep(j, n) {
if(i*n+j < 14) cout << p[i*n+j] << " ";
else cout << i*n+j << " ";
}
cout << '\n';
}
rep(i, n) {
rep(j, n) {
if(i*n+j < 14) cout << q[i*n+j] << " ";
else cout << i*n+j << " ";
}
cout << '\n';
}
return 0;
}
res[score] = p;
}
}
return 0;
}
Yudai0606Nazo