#abc265c. C - Belt Conveyor

C - Belt Conveyor

Score : 300300 points

问题描述

我们有一个包含 HH 行(水平方向)和 WW 列(垂直方向)的网格。(i,j)(i, j) 表示从顶部数第 ii 行、从左边数第 jj 列的方格。
(i,j)(i,j) 上有一个字符 Gi,jG_{i,j}Gi,jG_{i,j} 可以是 UDLR

你初始位于 (1,1)(1,1)。重复执行以下操作,直到无法移动为止。

(i,j)(i,j) 为当前所在的方格。
Gi,jG_{i,j}Ui1i \neq 1,则移动到 (i1,j)(i-1,j)
Gi,jG_{i,j}DiHi \neq H,则移动到 (i+1,j)(i+1,j)
Gi,jG_{i,j}Lj1j \neq 1,则移动到 (i,j1)(i,j-1)
Gi,jG_{i,j}RjWj \neq W,则移动到 (i,j+1)(i,j+1)
否则,你无法进行移动。

当你无法移动时,输出最终停留的方格坐标。
如果无限循环移动,则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a grid with HH horizontal rows and WW vertical columns. (i,j)(i, j) denotes the square at the ii-th row from the top and jj-th column from the left.
(i,j)(i,j) has a character Gi,jG_{i,j} written on it. Gi,jG_{i,j} is U, D, L, or R.

You are initially at (1,1)(1,1). You repeat the following operation until you cannot make a move.

Let (i,j)(i,j) be the square you are currently at.
If Gi,jG_{i,j} is U and i1i \neq 1, move to (i1,j)(i-1,j).
If Gi,jG_{i,j} is D and iHi \neq H, move to (i+1,j)(i+1,j).
If Gi,jG_{i,j} is L and j1j \neq 1, move to (i,j1)(i,j-1).
If Gi,jG_{i,j} is R and jWj \neq W, move to (i,j+1)(i,j+1).
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

  • 1H,W5001 \leq H, W \leq 500
  • Gi,jG_{i,j} is U, D, L, or R.
  • HH and WW are integers.

Input

Input is given from Standard Input in the following format:

HH WW

G1,1G1,2G1,WG_{1,1}G_{1,2}\dots G_{1,W}

G2,1G2,2G2,WG_{2,1}G_{2,2}\dots G_{2,W}

\vdots

GH,1GH,2GH,WG_{H,1}G_{H,2}\dots G_{H,W}

Output

If you end up at (i,j)(i, j), print it in the following format:

ii jj

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)(1,2)(2,2)(2,3)(1,3)(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1,3)(1, 3).

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