結果

問題 No.5022 XOR Printer
ユーザー Akidai
提出日時 2025-07-26 13:31:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,348 bytes
コンパイル時間 4,452 ms
コンパイル使用メモリ 258,360 KB
実行使用メモリ 7,720 KB
スコア 3,860,393,016
最終ジャッジ日時 2025-07-26 13:31:16
合計ジャッジ時間 7,079 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = (1LL << 62) - (1LL << 31) - 1;

#define REP(i, init, n) for(int i = (int)(init); i < (int)(n); i++)
#define RREP(i, init, n) for(int i = (int)(init); i >= (int)(n); i--)
#define All(A) A.begin(), A.end()
#define rAll(A) A.rbegin(), A.rend()

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>

int N, T;
vvi A;

int calc_dist(const pint &a, const pint &b) {
    return abs(a.first - b.first) + abs(a.second - b.second);
}

void move_U(pint &cur, string &ans) {
    cur.first--;
    ans += 'U';
}
void move_D(pint &cur, string &ans) {
    cur.first++;
    ans += 'D';
}
void move_L(pint &cur, string &ans) {
    cur.second--;
    ans += 'L';
}
void move_R(pint &cur, string &ans) {
    cur.second++;
    ans += 'R';
}
void write(int &s, pint &cur, string &ans) {
    A[cur.first][cur.second] ^= s;
    ans += 'W';
}
void copy(int &s, pint &cur, string &ans) {
    s ^= A[cur.first][cur.second];
    ans += 'C';
}
void move(pint &cur, const pint &next, string &ans) {
    while(cur != next) {
        if(cur.first < next.first) {
            move_D(cur, ans);
        } else if(cur.first > next.first) {
            move_U(cur, ans);
        } else if(cur.second < next.second) {
            move_R(cur, ans);
        } else if(cur.second > next.second) {
            move_L(cur, ans);
        }
    }
}


void solve() {
    pint cur = {0, 0};
    int s = 0;
    int move_count = 0;
    string ans = "";

    int mask = (1 << 19);
    int min_dist = 100;
    pint next_target = {-1, -1};
    REP(i, 0, N) {
        REP(j, 0, N) {
            if(A[i][j] & mask) {
                pint next = {i, j};
                int dist = calc_dist(cur, next);
                if(dist < min_dist) {
                    min_dist = dist;
                    next_target = next;
                }
            }
        }
    }

    move(cur, next_target, ans);
    copy(s, cur, ans);

    vvi board(N, vi(N, 0));
    int rest = 0;
    REP(i, 0, N) {
        REP(j, 0, N) {
            if(A[i][j] & mask) {
                board[i][j] = 1;
                rest++;
            }
        }
    }
    while(rest > 0) {
        int min_dist = 100;
        pint next = {-1, -1};
        REP(i, 0, N) {
            REP(j, 0, N) {
                if(board[i][j] == 0) {
                    int dist = calc_dist(cur, {i, j});
                    if(dist < min_dist) {
                        min_dist = dist;
                        next = {i, j};
                    }
                }
            }
        }

        if(next.first == -1) {
            break; // No more targets
        }
        move(cur, next, ans);
        if(ans.size() >= T) break;

        write(s, cur, ans);
        board[next.first][next.second] = 1;
        rest--;
    }

    if(ans.size() >= T) {
        ans = ans.substr(0, T);
    }
    for(char c: ans) {
        cout << c << endl;
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    cin >> N >> T;
    A.resize(N, vi(N));
    REP(i, 0, N) {
        REP(j, 0, N) {
            cin >> A[i][j];
        }
    }
    solve();
}
0