#abc265c. [abc265_c]Belt Conveyor
[abc265_c]Belt Conveyor
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 $(1, 1) \\to (1, 2) \\to (2, 2) \\to (2, 3) \\to (1, 3)$, 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