結果

問題 No.5022 XOR Printer
ユーザー kaede2020
提出日時 2025-07-26 13:24:34
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,952 ms / 2,000 ms
コード長 2,558 bytes
コンパイル時間 3,256 ms
コンパイル使用メモリ 292,076 KB
実行使用メモリ 7,716 KB
スコア 3,242,588,476
最終ジャッジ日時 2025-07-26 13:26:20
合計ジャッジ時間 105,211 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// グローバル変数
int N, T;
vector<vector<int>> A0;

// 操作一覧
const vector<char> cmds = {'U','D','L','R','W','C'};

// 盤面外への移動判定
bool can_move(int x, int y, char c){
    if(c == 'U') return x > 0;
    if(c == 'D') return x < N-1;
    if(c == 'L') return y > 0;
    if(c == 'R') return y < N-1;
    return true; // W, C は常に有効
}

// コマンド適用
void apply(char c, vector<vector<int>>& A, int& x, int& y, int& s){
    switch(c){
        case 'U': x--; break;
        case 'D': x++; break;
        case 'L': y--; break;
        case 'R': y++; break;
        case 'W': A[x][y] ^= s; break;
        case 'C': s ^= A[x][y]; break;
    }
}

// スコア計算(愚直)
ll calc_score(const vector<vector<int>>& A){
    ll sum = 0;
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            sum += A[i][j];
    return sum;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // 入力
    cin >> N >> T;
    A0.assign(N, vector<int>(N));
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++)
            cin >> A0[i][j];

    // 乱数初期化
    mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> dist(0, (int)cmds.size() - 1);

    // 時間制御(約1.95秒)
    auto start = chrono::steady_clock::now();
    auto limit = start + chrono::milliseconds(1950);

    ll best_score = LLONG_MIN;
    vector<char> best_ops;

    // 時間内にランダムシミュレート
    while(chrono::steady_clock::now() < limit){
        vector<vector<int>> A = A0;
        int x = 0, y = 0, s = 0;
        vector<char> ops;
        ops.reserve(T);

        // T 回までの操作列を試す
        for(int t = 0; t < T; t++){
            char c;
            do {
                c = cmds[dist(mt)];
            } while(!can_move(x, y, c));

            apply(c, A, x, y, s);
            ops.push_back(c);

            // **ここで中間スコアを評価!**
            ll sc = calc_score(A);
            if(sc > best_score){
                best_score = sc;
                best_ops = ops;  // 最良時のプレフィックスを保持
                cerr << "New best score: " << best_score << " with ops: ";
            }
        }
    }

    // 最終的に得られた最良の操作列を出力
    for(char c : best_ops){
        cout << c << "\n";
    }
    cerr << "Final best score: " << best_score << "\n";
    return 0;
}
0