A15708 | Following Directions
来源USACO / 2023
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alperen is standing at the point $(0,0)$ . He is given a string $s$ of length $n$ and performs $n$ moves. The $i$ -th move is as follows:
- if $s_i = \texttt{L}$ , then move one unit left;
- if $s_i = \texttt{R}$ , then move one unit right;
- if $s_i = \texttt{U}$ , then move one unit up;
- if $s_i = \texttt{D}$ , then move one unit down.
 If Alperen starts at the center point, he can make the four moves shown. There is a candy at $(1,1)$ (that is, one unit above and one unit to the right of Alperen's starting point). You need to determine if Alperen ever passes the candy.  Alperen's path in the first test case.
- if $s_i = \texttt{L}$ , then move one unit left;
- if $s_i = \texttt{R}$ , then move one unit right;
- if $s_i = \texttt{U}$ , then move one unit up;
- if $s_i = \texttt{D}$ , then move one unit down.
 If Alperen starts at the center point, he can make the four moves shown. There is a candy at $(1,1)$ (that is, one unit above and one unit to the right of Alperen's starting point). You need to determine if Alperen ever passes the candy.  Alperen's path in the first test case.
输入格式
The first line of the input contains an integer $t$ ( $1 \leq t \leq 1000$ ) — the number of testcases.
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 50$ ) — the length of the string.
The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{L}$ , $\texttt{R}$ , $\texttt{D}$ , and $\texttt{U}$ , denoting the moves Alperen makes.
The first line of each test case contains an integer $n$ ( $1 \leq n \leq 50$ ) — the length of the string.
The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{L}$ , $\texttt{R}$ , $\texttt{D}$ , and $\texttt{U}$ , denoting the moves Alperen makes.
输出格式
For each test case, output "YES" (without quotes) if Alperen passes the candy, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
输入输出样例
输入 #1
7 7 UUURDDL 2 UR 8 RRRUUDDD 3 LLL 4 DUUR 5 RUDLL 11 LLLLDDRUDRD
输出 #1
YES YES NO NO YES YES NO
In the first test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{U}} {\to} (0, 1) \overset{\texttt{U}} {\to} (0, 2) \overset{\texttt{U}} {\to} (0, 3) \overset{\texttt{R}} {\to} (1, 3) \overset{\texttt{D}} {\to} (1, 2) \overset{\texttt{D}} {\to} {\color{green} \mathbf{(1, 1)}} \overset{\texttt{L}} {\to} (0, 1). $$
Note that Alperen doesn't need to end at the candy's location of $(1, 1)$, he just needs to pass it at some point.
In the second test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{U}} {\to} (0, 1) \overset{\texttt{R}} {\to} {\color{green} \mathbf{(1, 1)}}. $$
In the third test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{R}} {\to} (1, 0) \overset{\texttt{R}} {\to} (2, 0) \overset{\texttt{R}} {\to} (3, 0) \overset{\texttt{U}} {\to} (3, 1) \overset{\texttt{U}} {\to} (3, 2) \overset{\texttt{D}} {\to} (3, 1) \overset{\texttt{D}} {\to} (3, 0) \overset{\texttt{D}} {\to} (3, -1). $$
In the fourth test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{L}} {\to} (-1, 0) \overset{\texttt{L}} {\to} (-2, 0) \overset{\texttt{L}} {\to} (-3, 0). $$
$$ (0, 0) \overset{\texttt{U}} {\to} (0, 1) \overset{\texttt{U}} {\to} (0, 2) \overset{\texttt{U}} {\to} (0, 3) \overset{\texttt{R}} {\to} (1, 3) \overset{\texttt{D}} {\to} (1, 2) \overset{\texttt{D}} {\to} {\color{green} \mathbf{(1, 1)}} \overset{\texttt{L}} {\to} (0, 1). $$
Note that Alperen doesn't need to end at the candy's location of $(1, 1)$, he just needs to pass it at some point.
In the second test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{U}} {\to} (0, 1) \overset{\texttt{R}} {\to} {\color{green} \mathbf{(1, 1)}}. $$
In the third test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{R}} {\to} (1, 0) \overset{\texttt{R}} {\to} (2, 0) \overset{\texttt{R}} {\to} (3, 0) \overset{\texttt{U}} {\to} (3, 1) \overset{\texttt{U}} {\to} (3, 2) \overset{\texttt{D}} {\to} (3, 1) \overset{\texttt{D}} {\to} (3, 0) \overset{\texttt{D}} {\to} (3, -1). $$
In the fourth test case, Alperen follows the path
$$ (0, 0) \overset{\texttt{L}} {\to} (-1, 0) \overset{\texttt{L}} {\to} (-2, 0) \overset{\texttt{L}} {\to} (-3, 0). $$
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted