結果

問題 No.3131 Twin Slide Puzzles
ユーザー Yudai0606Nazo
提出日時 2025-07-31 12:16:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,424 bytes
コンパイル時間 4,512 ms
コンパイル使用メモリ 266,392 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2025-07-31 12:16:34
合計ジャッジ時間 17,709 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 17 WA * 42
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
      |             ^~

ソースコード

diff #

#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;
        while(1) {
            shuffle(all(p), default_random_engine());
            if(!can_solve(p, n)) continue;
            ll score = calc(p, a);
            if(res.count(score)) {
                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;
}
0