CF2155B.Abraham's Great Escape
普及-
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Abraham is a brave explorer who goes where no other programmer has gone before. For his next expedition, he plans to investigate a peculiar maze. He knows that the maze is an n×n grid with an arrow in each cell that points in one of four directions: up, down, left and right. Abraham also knows that if he stands on an arrow, he will be forced to follow the arrows starting from that cell. Each arrow moves Abraham exactly 1 cell in the direction that it is pointing. If he reaches an arrow that points towards the outside of the maze, Abraham will escape the maze.
Abraham doesn't know how the arrows are arranged, so he wants to plan for multiple scenarios. He tasks you with finding an arrangement of arrows in the grid such that there are exactly k starting cells from which he can escape the maze.
亚伯拉罕是一位勇敢的探险家,他前往其他程序员从未涉足过的地方。在下一次探险中,他计划调查一座奇特的迷宫。他知道该迷宫是一个 n×n 的网格,每个格子中都有一枚箭头,指向四个方向之一:上、下、左、右。亚伯拉罕还知道,如果他站在某枚箭头上,他将被迫从该格子开始,沿着箭头指示的方向连续移动。每枚箭头恰好使亚伯拉罕向其所指方向移动 1 格。若他到达一枚指向迷宫外部的箭头,亚伯拉罕便成功逃出迷宫。
亚伯拉罕并不知道箭头的具体排布方式,因此他希望为多种情形提前规划。他委托你设计一种网格中的箭头排布方案,使得恰好有 k 个起始格子能让亚伯拉罕成功逃出迷宫。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The only line of each test case contains two integers n, k (2≤n≤100, 0≤k≤n2) — the size of the grid and the number of cells from which Abraham should be able to escape.
It is guaranteed that the sum of n2 over all test cases does not exceed 105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤1000)。随后是各测试用例的描述。
每个测试用例仅有一行,包含两个整数 n、k(2≤n≤100,0≤k≤n2)——分别表示网格的大小以及亚伯拉罕应能从中逃脱的格子数量。
保证所有测试用例的 n2 之和不超过 105。
输出格式
For each test case, do one of the following:
- If there exists a grid satisfying the requirement, print YES and then print n lines with n characters in each line indicating the direction of the arrows. Each character should be one of U (up), R (right), L (left), or D (down).
- Otherwise, declare that the task is impossible by printing NO.
If there are multiple solutions, print any of them.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
对于每个测试用例,请执行以下操作之一:
- 如果存在满足要求的网格,则输出
YES,然后输出 n 行,每行包含 n 个字符,表示各格子中箭头的方向。每个字符必须是U(上)、R(右)、L(左)或D(下)之一。 - 否则,输出
NO,表明该任务不可能完成。
若存在多个可行解,输出任意一个即可。
你可以以任意大小写形式输出答案(大写或小写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答。
输入输出样例
输入#1
3 2 4 3 5 2 3
输出#1
YES UU UU YES UUU RDR ULR NO
说明/提示
In the first test case, no matter which cell Abraham stands in initially, he will eventually exit the maze as he will move upward successively; thus, he can escape from all 4 cells, as required.
In the second test case, Abraham eventually escapes if he stands on one of the following:
- Any cell in the first row (all of which are U)
- Any cell in the third column (one of which is U and the other two R)
There is no other cell where he can stand and escape. We see that Abraham can escape from exactly 5 cells in this arrangement, as required.
In the third test case, it can be proved that there's no arrangement of arrows so that Abraham can escape from exactly 3 cells.
在第一个测试用例中,无论亚伯拉罕最初站在哪个格子中,他最终都会走出迷宫,因为他将连续向上移动;因此,他可以从全部 4 个格子中逃脱,符合题目要求。
在第二个测试用例中,若亚伯拉罕站在以下任一格子中,他最终可以逃脱:
- 第一行中的任意格子(这些格子均标有 U)
- 第三列中的任意格子(其中一格标有 U,其余两格标有 R)
不存在其他格子使得他能站在其上并成功逃脱。我们发现,在该布局下亚伯拉罕恰好能从 5 个格子中逃脱,符合题目要求。
在第三个测试用例中,可以证明:不存在一种箭头布局,使得亚伯拉罕恰好能从 3 个格子中逃脱。
输入解题思路,AI测评打分。不知道怎么写?