LRUD 移动 2AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)You are given positive integers $N$ and $K$.
给定正整数 $N$ 和 $K$。AI (CI翻译)
There is an $N\times N$ grid. The cell at the $r$-th row from the top and the $c$-th column from the left is denoted as cell $(r,c)$.
有一个 $N\times N$ 的网格。从上往下数第 $r$ 行、从左往右数第 $c$ 列的格子记为格子 $(r,c)$。AI (CI翻译)
Initially, a piece is placed on cell $(1,1)$.
初始时,一个棋子位于格子 $(1,1)$。AI (CI翻译)
You will perform the following operation exactly $N^2-1$ times so that the piece ends up in cell $(N,N)$:
你需要恰好进行 $N^2-1$ 次以下操作,使棋子最终到达格子 $(N,N)$:AI (CI翻译)
- Move the piece one cell to a cell vertically or horizontally adjacent to the cell it is currently on.
- 将棋子移动到与当前格子上下或左右相邻的一个格子。
Here, over the course of the movement, each of the $N^2$ cells must be visited exactly once. Cell $(1,1)$, where the piece is initially placed, is considered visited.
在移动过程中,必须恰好访问每个格子一次。棋子初始所在的格子 $(1,1)$ 也视为已访问。AI (CI翻译)
Determine whether there exists a sequence of operations where the piece moves one cell to the right exactly $K$ times, and if it exists, find one such sequence.
判断是否存在一种操作序列,使棋子恰好向右移动 $K$ 次;如果存在,请找出这样一个序列。AI (CI翻译)
You are given $T$ test cases; solve each of them.
给定 $T$ 组测试数据,请分别求解。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$1\le T\le 5\times 10^3$$2\le N\le 10^3$$0\le K\le N^2-1$- The sum of
$N^2$over all test cases is at most$10^6$. - All input values are integers.
$1\le T\le 5\times 10^3$$2\le N\le 10^3$$0\le K\le N^2-1$- 所有测试数据中
$N^2$的总和不超过$10^6$。 - 所有输入值均为整数。
Input
输入格式
AI (CI翻译)The input is given from Standard Input in the following format:
输入通过标准输入给出,格式如下:AI (CI翻译)
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_TAI (CI翻译)
Each test case is given in the following format:
每组测试数据的格式如下:AI (CI翻译)
N K
N KAI (CI翻译)
Output
输出格式
AI (CI翻译)Output the answers for the test cases in order, separated by newlines.
按顺序输出各组测试数据的答案,每组答案之间用换行符分隔。AI (CI翻译)
For each test case, if there is no sequence of operations satisfying the condition, output No.
对于每组测试数据,如果不存在满足条件的操作序列,输出 No。AI (CI翻译)
If there exists a sequence of operations satisfying the condition, output it in the following format:
如果存在满足条件的操作序列,按以下格式输出:AI (CI翻译)
Yes
S_1S_2\dots S_{N^2-1}
Yes
S_1S_2\dots S_{N^2-1}AI (CI翻译)
Here, $S_k$ represents the $k$-th move, and is one of the following:
其中,$S_k$ 表示第 $k$ 次移动,取以下字符之一:AI (CI翻译)
$S_k=$Lif the piece moves one cell to the left$S_k=$Rif the piece moves one cell to the right$S_k=$Uif the piece moves one cell up$S_k=$Dif the piece moves one cell down
- 如果棋子向左移动一格,则
$S_k=$L - 如果棋子向右移动一格,则
$S_k=$R - 如果棋子向上移动一格,则
$S_k=$U - 如果棋子向下移动一格,则
$S_k=$D
If there are multiple sequences of operations satisfying the condition, any of them will be accepted.
如果有多个满足条件的操作序列,输出其中任意一个即可。AI (CI翻译)
样例
3
3 4
2 1
5 10
Yes
RRDLLDRR
No
Yes
RRRRDDDLLLURRULLLDDDRRRR
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。