/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 333 点
問題文
H 行 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
各マス (i, j) には文字 C_{i, j} が書かれており、それぞれ以下の方向を表しています。
U: 上方向、すなわちマス (i - 1, j)D: 下方向、すなわちマス (i + 1, j)L: 左方向、すなわちマス (i, j - 1)R: 右方向、すなわちマス (i, j + 1)
高橋君は最初、マス (1, 1) にいます。
高橋君は現在いるマスに書かれた文字が指す方向の隣接するマスへ移動することを繰り返します。
ただし、移動先がマス目の外側になってしまう場合、高橋君は移動を行わずにそのマスで移動を終了します。
高橋君の移動が最終的に終了するかどうかを判定し、終了する場合は終了するまでに行った移動の回数を出力してください。
同じマスを再び訪れるなどして移動が永遠に終了しない場合は -1 を出力してください。
制約
- 1 \leq H \leq 2000
- 1 \leq W \leq 500
- H, W は整数
- C_{i, j} は
U,D,L,Rのいずれかである (1 \leq i \leq H,\ 1 \leq j \leq W)
入力
入力は以下の形式で標準入力から与えられる。
H W
C_{1,1}C_{1,2} \dots C_{1,W}
C_{2,1}C_{2,2} \dots C_{2,W}
:
C_{H,1}C_{H,2} \dots C_{H,W}
- 1 行目には、マス目の行数を表す H と列数を表す W が、スペース区切りで与えられる。
- 2 行目から H 行にわたって、各マスに書かれた文字を表す文字列が与えられる。
- 1 + i 行目には、長さ W の文字列が与えられ、その j 文字目はマス (i, j) に書かれた文字 C_{i, j} である。
出力
高橋君の移動が終了する場合は終了するまでに行った移動の回数を、移動が永遠に終了しない場合は -1 を 1 行に出力せよ。
入力例 1
3 4 RRRD ULLD UURR
出力例 1
5
入力例 2
2 2 RD UL
出力例 2
-1
入力例 3
6 6 RRRRRD DLLLLL RRRRRD DLLLLL RRRRRD LLLLLL
出力例 3
35
入力例 4
20 20 RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL
出力例 4
399
入力例 5
1 1 U
出力例 5
0
Score : 333 pts
Problem Statement
There is a grid with H rows and W columns. Let square (i, j) denote the square at the i-th row from the top and j-th column from the left.
Each square (i, j) has a character C_{i, j} written on it, which represents the following directions:
U: Up, that is, square (i - 1, j)D: Down, that is, square (i + 1, j)L: Left, that is, square (i, j - 1)R: Right, that is, square (i, j + 1)
Takahashi is initially at square (1, 1).
He repeatedly moves to the adjacent square in the direction indicated by the character written on the square he is currently on.
However, if the destination would be outside the grid, Takahashi does not move and terminates his movement at that square.
Determine whether Takahashi's movement eventually terminates. If it terminates, output the number of moves performed until it terminates.
If the movement never terminates, such as by visiting the same square again, output -1.
Constraints
- 1 \leq H \leq 2000
- 1 \leq W \leq 500
- H and W are integers.
- C_{i, j} is
U,D,L, orR(1 \leq i \leq H,\ 1 \leq j \leq W).
Input
The input is given from Standard Input in the following format:
H W
C_{1,1}C_{1,2} \dots C_{1,W}
C_{2,1}C_{2,2} \dots C_{2,W}
:
C_{H,1}C_{H,2} \dots C_{H,W}
- The first line contains H, the number of rows of the grid, and W, the number of columns, separated by a space.
- The next H lines starting from the second line give strings representing the characters written on each square.
- The (1 + i)-th line contains a string of length W, whose j-th character is the character C_{i, j} written on square (i, j).
Output
If Takahashi's movement terminates, print the number of moves performed until it terminates. If the movement never terminates, print -1 in a single line.
Sample Input 1
3 4 RRRD ULLD UURR
Sample Output 1
5
Sample Input 2
2 2 RD UL
Sample Output 2
-1
Sample Input 3
6 6 RRRRRD DLLLLL RRRRRD DLLLLL RRRRRD LLLLLL
Sample Output 3
35
Sample Input 4
20 20 RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL RRRRRRRRRRRRRRRRRRRD DLLLLLLLLLLLLLLLLLLL
Sample Output 4
399
Sample Input 5
1 1 U
Sample Output 5
0