結果

問題 No.3131 Twin Slide Puzzles
ユーザー Iroha_3856
提出日時 2025-04-25 20:55:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,114 bytes
コンパイル時間 3,820 ms
コンパイル使用メモリ 295,464 KB
実行使用メモリ 26,880 KB
最終ジャッジ日時 2025-04-25 20:55:50
合計ジャッジ時間 22,254 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 21 WA * 36
権限があれば一括ダウンロードができます
コンパイルメッセージ
In lambda function,
    inlined from ‘int main()’ at main.cpp:47:21:
main.cpp:31:17: warning: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   31 |         return x%2 == y%2;
      |                ~^~
main.cpp: In function ‘int main()’:
main.cpp:22:13: note: ‘x’ was declared here
   22 |         int x, y;
      |             ^
In lambda function,
    inlined from ‘int main()’ at main.cpp:47:21:
main.cpp:31:24: warning: ‘y’ may be used uninitialized [-Wmaybe-uninitialized]
   31 |         return x%2 == y%2;
      |                       ~^~
main.cpp: In function ‘int main()’:
main.cpp:22:16: note: ‘y’ was declared here
   22 |         int x, y;
      |                ^
In lambda function,
    inlined from ‘int main()’ at main.cpp:68:21:
main.cpp:31:17: warning: ‘x’ may be used uninitialized [-Wmaybe-uninitialized]
   31 |         return x%2 == y%2;
      |                ~^~
main.cpp: In function ‘int main()’:
main.cpp:22:13: note: ‘x’ was declared here
   22 |         int x, y;
      |             ^
In lambda function,
    inlined from ‘int main()’ at main.cpp:68:21:
main.cpp:31:24: warning: ‘y’ may be used uninitialized [-Wmaybe-uninitialized]
   31 |         return x%2 == y%2;
      |                       ~^~
main.cpp: In function ‘int main()’:
main.cpp:22:16: note: ‘y’ was declared here
   22 |         int x, y;
      |                ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/dsu>

#define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++)
#define ll long long
#define siz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()

int main() {
    int N; cin >> N;
    vector<ll> A(N*N);
    rep(i, 0, N*N) cin >> A[i];
    auto score = [&](vector<int> P) {
        ll ret = 0;
        rep(i, 0, siz(P)) {
            ret += A[i] * P[i];
        }
        return ret;
    };
    auto isok = [&](vector<int> P) {
        int x, y;
        rep(i, 0, siz(P)) {
            if (P[i] == 0) {
                x = i/N+i%N;
            }
        }
        rep(i, 0, siz(P)) rep(j, i+1, siz(P)) {
            if (P[i] > P[j]) y++;
        }
        return x%2 == y%2;
    };
    auto print = [&](vector<int> P) {
        vector<vector<int>> ans(N, vector<int>(N));
        rep(i, 0, N) rep(j, 0, N) ans[i][j] = i*N+j;
        rep(i, 0, siz(P)) ans[i/N][i%N] = P[i];
        rep(i, 0, N) {
            rep(j, 0, N) cout << ans[i][j] << " ";
            cout << endl;
        }
    };
    if (N <= 3) {
        vector<int> P(N*N);
        iota(all(P), 0);
        map<ll, vector<int>> mp;
        do {
            if (isok(P)) {
                int sc = score(P);
                if (mp.contains(sc) and mp[sc] != P) {
                    cout << "Yes" << endl;
                    print(mp[sc]);
                    print(P);
                    return 0;
                }
                mp[sc] = P;
            }
        }while(next_permutation(all(P)));
        cout << "No" << endl;
    }
    else {
        map<ll, vector<int>> mp;
        vector<int> P(16);
        iota(all(P), 0);
        random_device seed;
        mt19937 rnd(seed());
        while(true) {
            shuffle(all(P), rnd);
            if (isok(P)) {
                int sc = score(P);
                if (mp.contains(sc) and mp[sc] != P) {
                    cout << "Yes" << endl;
                    print(mp[sc]);
                    print(P);
                    return 0;
                }
                mp[sc] = P;
            }
        }
    }
}
0