// baseline score: 0 // improved score: 808204780 // score delta: +808204780 // スネーク順に盤面を巡回し、距離3以内から2マス選んでXORで更新する貪欲解法 #include using namespace std; struct Input { int N, T; vector grid; // flattened N*N grid }; struct Result { long long score; string output; }; const int D = 3; // search distance inline void move_to(int &ci, int &cj, int ti, int tj, string &ops, int &steps){ while(ci < ti){ ops += "D\n"; ++ci; ++steps; } while(ci > ti){ ops += "U\n"; --ci; ++steps; } while(cj < tj){ ops += "R\n"; ++cj; ++steps; } while(cj > tj){ ops += "L\n"; --cj; ++steps; } } Result solve(const Input &in){ const int N = in.N; const int T = in.T; vector board = in.grid; // copy auto idx = [&](int x,int y){ return x*N + y; }; vector> order; order.reserve(N*N); for(int r=0;r=0;c--) order.emplace_back(r,c); } } int ci=0,cj=0; // current position int s=0; // register int steps=0; string ops; ops.reserve(T); for(auto [x,y] : order){ if(steps >= T) break; // candidate cells within manhattan distance D vector> cand; for(int dx=-D; dx<=D; ++dx){ for(int dy=-D; dy<=D; ++dy){ int nx=x+dx, ny=y+dy; if(nx<0||nx>=N||ny<0||ny>=N) continue; if(abs(dx)+abs(dy) > D) continue; cand.emplace_back(nx,ny); } } pair best_a = {x,y}; pair best_b = {x,y}; int best_val = -1; for(size_t i=0;i best_val){ best_val = val; best_a = cand[i]; best_b = cand[j]; } } } // execute moves and operations move_to(ci,cj,best_a.first,best_a.second,ops,steps); if(steps>=T) break; ops += "C\n"; s ^= board[idx(best_a.first,best_a.second)]; ++steps; if(steps>=T) break; move_to(ci,cj,best_b.first,best_b.second,ops,steps); if(steps>=T) break; ops += "C\n"; s ^= board[idx(best_b.first,best_b.second)]; ++steps; if(steps>=T) break; move_to(ci,cj,x,y,ops,steps); if(steps>=T) break; ops += "W\n"; board[idx(x,y)] ^= s; ++steps; cerr<<"s="<=T) break; } // compute score long long total=0; for(int v:board) total+=v; return {total, ops}; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); Input in; if(!(cin>>in.N>>in.T)) return 0; in.grid.resize(in.N*in.N); for(int i=0;i>in.grid[i]; auto res=solve(in); cout<