結果

問題 No.5022 XOR Printer
ユーザー yimiya(いみや)
提出日時 2025-07-26 14:21:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 2,871 ms
コンパイル使用メモリ 281,764 KB
実行使用メモリ 7,720 KB
スコア 2,642,651,897
最終ジャッジ日時 2025-07-26 14:21:21
合計ジャッジ時間 5,171 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

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

ll cnt = 0;
ll MAX_OP = 1000;

bool move(ll &sx, ll &sy, ll ex, ll ey) {
    ll dx = abs(sx - ex);
    ll dy = abs(sy - ey);
    if (cnt + dx + dy > MAX_OP) return false;
    for (int i = 0; i < dx; ++i) cout << (ex > sx ? "R" : "L") << "\n";
    for (int i = 0; i < dy; ++i) cout << (ey > sy ? "D" : "U") << "\n";
    cnt += dx + dy;
    sx = ex;
    sy = ey;
    return true;
}

bool copy(ll x, ll y, ll &num, const vector<vector<ll>> &grid) {
    if (cnt + 1 > MAX_OP) return false;
    num ^= grid[x][y];
    cout << "C\n";
    ++cnt;
    return true;
}

ll total_score(const vector<vector<ll>>& grid) {
    ll sum = 0;
    for (const auto &row : grid) {
        for (ll val : row) sum += val;
    }
    return sum;
}

bool write(ll x, ll y, ll num, vector<vector<ll>> &grid) {
    if (cnt + 1 > MAX_OP) return false;
    ll before = grid[x][y];
    ll after = before ^ num;

    // 一時的に変更
    grid[x][y] = after;
    ll new_score = total_score(grid);
    grid[x][y] = before;  // 元に戻す

    // 現状のスコアを計算(注意:頻繁に呼ぶなら高速化要検討)
    ll old_score = total_score(grid);

    // スコア増加があれば確定
    if (new_score > old_score) {
        grid[x][y] = after;
        cout << "W\n";
        ++cnt;
        return true;
    }
    // スコア悪化はスキップ
    return true;
}

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

    ll N, T;
    cin >> N >> T;
    vector<vector<ll>> grid(N, vector<ll>(N));
    for (int i = 0; i < N; ++i)
        for (int j = 0; j < N; ++j)
            cin >> grid[i][j];

    ll s = 0;
    ll x = 0, y = 0;

    // 全ビット範囲を処理(0〜19)
    for (int p = 10; p < 20; ++p) {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                // pより上位ビットが立ってたらスキップ
                bool skip = false;
                for (int q = 19; q > p; --q) {
                    if (grid[i][j] & (1LL << q)) {
                        skip = true;
                        break;
                    }
                }
                if (skip) continue;

                if (grid[i][j] & (1LL << p)) {
                    if (!move(x, y, i, j)) return 0;
                    if (!copy(x, y, s, grid)) return 0;
                    if (!move(x, y, 0, 0)) return 0;

                    for (int a = 0; a < N; ++a) {
                        for (int b = 0; b < N; ++b) {
                            if ((grid[a][b] & (1LL << p)) == 0) {
                                if (!move(x, y, a, b)) return 0;
                                if (!write(x, y, s, grid)) return 0;
                            }
                        }
                    }

                    if (!move(x, y, i, j)) return 0;
                    if (!copy(x, y, s, grid)) return 0;
                    goto next;
                }
            }
        }
        next:;
    }
}
0