// Baseline Score: 0 // Improved Score: 1524098986 // Improvement: 1524098986 // XOR Printer greedy solver - snake traversal with local improvements. // Each cell is visited in snake order. For the current cell x, search within // distance d (3) for one or two cells a,b that maximise x^s, where s accumulates // values of selected cells using 'C'. After performing best action we write 'W' // to apply. Stop when operation count would exceed the limit (1000). #include using namespace std; struct Input { int N, T; vector> grid; }; struct Pos {int r, c;}; // Manhattan distance inline int dist(const Pos& a, const Pos& b){ return abs(a.r - b.r) + abs(a.c - b.c); } // Move from pos a to b, append ops void append_move(Pos &cur, const Pos &to, string &ops){ while(cur.r < to.r){ ops += 'D'; cur.r++; } while(cur.r > to.r){ ops += 'U'; cur.r--; } while(cur.c < to.c){ ops += 'R'; cur.c++; } while(cur.c > to.c){ ops += 'L'; cur.c--; } } pair solve(const Input& in){ const int N=in.N; const int T=in.T; const int D=3; vector> G=in.grid; string ops; ops.reserve(T); Pos cur{0,0}; long long s=0; vector path; path.reserve(N*N); for(int r=0;r=0;c--) path.push_back({r,c}); } } auto inside=[&](int r,int c){return 0<=r && r T) break; append_move(cur,x,ops); long long bestVal=G[x.r][x.c]; int bestType=0; // 0 none,1 one cell,2 two cells Pos bestA,bestB; int bestCost=0; long long bestS=s; // gather candidate cells within D vector cells; for(int dr=-D;dr<=D;dr++) for(int dc=-D;dc<=D;dc++) if(abs(dr)+abs(dc)<=D && inside(x.r+dr,x.c+dc)) cells.push_back({x.r+dr,x.c+dc}); for(auto a:cells){ int c1 = dist(x,a)*2 + 2; // x->a->x plus C,W if((int)ops.size()+c1 > T) continue; long long ns = s ^ G[a.r][a.c]; long long val = G[x.r][x.c] ^ ns; if(val>bestVal){ bestVal=val; bestType=1; bestA=a; bestCost=c1; bestS=ns; } } for(size_t i=0;ia->b->x, C,C,W if((int)ops.size()+c2 > T) continue; long long ns = s ^ G[a.r][a.c] ^ G[b.r][b.c]; long long val = G[x.r][x.c] ^ ns; if(val>bestVal){ bestVal=val; bestType=2; bestA=a; bestB=b; bestCost=c2; bestS=ns; } } if(bestType!=0 && (int)ops.size()+bestCost <= T){ if(bestType==1){ append_move(cur,bestA,ops); ops+='C'; append_move(cur,x,ops); ops+='W'; }else{ append_move(cur,bestA,ops); ops+='C'; append_move(cur,bestB,ops); ops+='C'; append_move(cur,x,ops); ops+='W'; } s=bestS; G[x.r][x.c]=bestVal; } idx++; if(idx>=path.size()) break; Pos nxt=path[idx]; int mv = dist(cur,nxt); if((int)ops.size()+mv > T) break; append_move(cur,nxt,ops); } long long total=0; for(auto &row:G) for(auto v:row) total+=v; // convert operations to whitespace-separated form string out; out.reserve(2*ops.size()); for(char c:ops){ out.push_back(c); out.push_back(' '); } if(!out.empty()) out.pop_back(); return {total, out}; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); Input in; if(!(cin>>in.N>>in.T)) return 0; in.grid.assign(in.N, vector(in.N)); for(int i=0;i>in.grid[i][j]; auto [score, out] = solve(in); cout<