#include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace std; typedef long long ll; pair solve(vector> a, int n, int t) { auto b = a; int hand = 0; int ni = 0, nj = 0; string ans; auto move = [&](int i, int j) { while (ni > i) { ans += 'U'; ni--; } while (ni < i) { ans += 'D'; ni++; } while (nj > j) { ans += 'L'; nj--; } while (nj < j) { ans += 'R'; nj++; } ni = i; nj = j; }; auto copy = [&](int i, int j) { move(i, j); ans += 'C'; hand ^= a[i][j]; }; auto write = [&](int i, int j) { move(i, j); ans += 'W'; a[i][j] ^= hand; }; auto update = [&](int i, int j) { if ((a[i][j] ^ hand) > a[i][j]) { write(i, j); } }; rep(i, 0, n) rep(j, 0, n) { if ((hand == 0) && ((a[i][j] >> 19) & 1)) { copy(i, j); } } rep(i, 0, n) { if (i % 2 == 0) { for (int j = 0; j < n; j++) { if (!((a[i][j] >> 19) & 1)) { write(i, j); } } } else { for (int j = n - 1; j >= 0; j--) { if (!((a[i][j] >> 19) & 1)) { write(i, j); } } } } for (int d = 18; d >= 0; d--) { vector> exclusion(n, vector(n)); int mask = (1 << 20) - (1 << (d + 2)); rep(i, 0, n) rep(j, 0, n) { if ((a[i][j] & mask) != mask) { exclusion[i][j] = 1; } } int one_i, one_j, one_dist = 1e9; int zero_i, zero_j, zero_dist = 1e9; rep(i, 0, n) rep(j, 0, n) { if (exclusion[i][j]) continue; int dist = abs(i - ni) + abs(j - nj); if ((a[i][j] >> d) & 1) { if (dist < one_dist) { one_dist = dist; one_i = i; one_j = j; } } else { if (dist < zero_dist) { zero_dist = dist; zero_i = i; zero_j = j; } } } if (one_dist == 1e9 || zero_dist == 1e9) { break; } write(one_i, one_j); copy(one_i, one_j); copy(zero_i, zero_j); if (d % 2 == 1) { for (int i = 0; i < n; i++) { if (i % 2 == 0) { for (int j = 0; j < n; j++) { update(i, j); } } else { for (int j = n - 1; j >= 0; j--) { update(i, j); } } } } else { for (int i = n - 1; i >= 0; i--) { if (i % 2 == 0) { for (int j = n - 1; j >= 0; j--) { update(i, j); } } else { for (int j = 0; j < n; j++) { update(i, j); } } } } } ans = ans.substr(0, t); auto calc_score = [&](vector> a, int n, string ans) { int ni = 0, nj = 0; int hand = 0; for (char c : ans) { if (c == 'U') ni--; else if (c == 'D') ni++; else if (c == 'L') nj--; else if (c == 'R') nj++; else if (c == 'C') hand ^= a[ni][nj]; else if (c == 'W') a[ni][nj] ^= hand; } int score = 0; rep(i, 0, n) rep(j, 0, n) { score += a[i][j]; } return score; }; return {calc_score(b, n, ans), ans}; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, t; cin >> n >> t; vector> a(n, vector(n)); rep(i, 0, n) rep(j, 0, n) cin >> a[i][j]; auto result = solve(a, n, t); rep(i, 0, n) rep(j, i + 1, n) swap(a[i][j], a[j][i]); auto result_transposed = solve(a, n, t); cerr << "Score: " << result.first << " vs " << result_transposed.first << '\n'; if (result.first > result_transposed.first) { for (auto c : result.second) { cout << c << '\n'; } } else { for (auto c : result_transposed.second) { if (c == 'R') c = 'D'; else if (c == 'D') c = 'R'; else if (c == 'L') c = 'U'; else if (c == 'U') c = 'L'; cout << c << '\n'; } } }