# 操作列のビームサーチ # 1回の操作を1ステップとしたビームサーチをする # 評価関数はスコア盤面の合計値 from __future__ import annotations import heapq import sys from dataclasses import dataclass, field from typing import List @dataclass(order=True) class BeamState: priority: int score: int = field(compare=False) board: List[List[int]] = field(compare=False) x: int = field(compare=False) y: int = field(compare=False) s: int = field(compare=False) ops: List[str] = field(compare=False) def board_sum(board: List[List[int]]) -> int: return sum(sum(row) for row in board) def apply_op(state: BeamState, op: str, n: int) -> BeamState | None: x, y, s_val = state.x, state.y, state.s board = [row[:] for row in state.board] score = state.score if op == "U": if x == 0: return None x -= 1 elif op == "D": if x == n - 1: return None x += 1 elif op == "L": if y == 0: return None y -= 1 elif op == "R": if y == n - 1: return None y += 1 elif op == "C": s_val ^= board[x][y] elif op == "W": old = board[x][y] new_val = old ^ s_val board[x][y] = new_val score += new_val - old else: return None ops = state.ops + [op] return BeamState(priority=-score, score=score, board=board, x=x, y=y, s=s_val, ops=ops) def beam_search(board: List[List[int]], t: int, width: int) -> List[str]: n = len(board) start_score = board_sum(board) start = BeamState( priority=-start_score, score=start_score, board=[row[:] for row in board], x=0, y=0, s=0, ops=[], ) beam = [start] for _ in range(t): candidates: list[tuple[int, BeamState]] = [] for state in beam: for op in ["U", "D", "L", "R", "W", "C"]: next_state = apply_op(state, op, n) if next_state is not None: heapq.heappush(candidates, (next_state.priority, next_state)) beam = [] for _ in range(min(width, len(candidates))): priority, st = heapq.heappop(candidates) beam.append(st) best = max(beam, key=lambda s: s.score) return best.ops def main() -> None: n, t = map(int, sys.stdin.readline().split()) board = [list(map(int, sys.stdin.readline().split())) for _ in range(n)] ops = beam_search(board, t, width=100) sys.stdout.write("\n".join(ops) + "\n") if __name__ == "__main__": main()