#abc265c. C - Belt Conveyor
C - Belt Conveyor
Score : points
问题描述
我们有一个包含 行(水平方向)和 列(垂直方向)的网格。 表示从顶部数第 行、从左边数第 列的方格。
在 上有一个字符 。 可以是 U
、D
、L
或 R
。
你初始位于 。重复执行以下操作,直到无法移动为止。
设 为当前所在的方格。
若 为U
且 ,则移动到 。
若 为D
且 ,则移动到 。
若 为L
且 ,则移动到 。
若 为R
且 ,则移动到 。
否则,你无法进行移动。
当你无法移动时,输出最终停留的方格坐标。
如果无限循环移动,则输出 -1
。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have a grid with horizontal rows and vertical columns. denotes the square at the -th row from the top and -th column from the left.
has a character written on it. is U
, D
, L
, or R
.
You are initially at . You repeat the following operation until you cannot make a move.
Let be the square you are currently at.
If isU
and , move to .
If isD
and , move to .
If isL
and , move to .
If isR
and , move to .
Otherwise, you cannot make a move.
Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1
instead.
Constraints
- is
U
,D
,L
, orR
. - and are integers.
Input
Input is given from Standard Input in the following format:
Output
If you end up at , print it in the following format:
If you indefinitely repeat moving, print -1
.
Sample Input 1
2 3
RDU
LRU
Sample Output 1
1 3
You will move as , ending up here, so the answer is .
Sample Input 2
2 3
RRD
ULL
Sample Output 2
-1
You will indefinitely repeat moving as $(1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots$, so -1
should be printed in this case.
Sample Input 3
9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
Sample Output 3
9 5
update @ 2024/3/10 11:12:07
相关
在以下作业中: