#abc341c. C - Takahashi Gets Lost
C - Takahashi Gets Lost
Score: points
问题描述
存在一个包含 行和 列的网格。
网格中的每个单元格是 陆地 或 海洋,由长度为 的 个字符串 表示。令 表示从上到下的第 行、从左到右的第 列的单元格,如果 的第 个字符是 .
, 那么 是陆地;若字符是 #
,则 是海洋。
约束条件保证了网格边缘的所有单元格(即满足至少一个条件:, , , )都是海洋。
高桥的宇宙飞船在网格中的某个单元格迫降。之后,他按照由长度为 的字符串 (包含 L
、R
、U
和 D
字符)表示的指令,在网格上移动了 次。对于 , 中的第 个字符如下描述第 次移动:
L
表示向左移动一格。即,如果他在移动前位于 ,那么移动后将位于 。R
表示向右移动一格。即,如果他在移动前位于 ,那么移动后将位于 。U
表示向上移动一格。即,如果他在移动前位于 ,那么移动后将位于 。D
表示向下移动一格。即,如果他在移动前位于 ,那么移动后将位于 。
已知沿他的路径的所有单元格(包括他迫降的单元格以及他当前所在的单元格)都不是海洋。输出可能成为他当前位置的单元格数量。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a grid with rows and columns.
Each cell of the grid is land or sea, which is represented by strings of length . Let denote the cell at the -th row from the top and -th column from the left, and is land if the -th character of is .
, and is sea if the character is #
.
The constraints guarantee that all cells on the perimeter of the grid (that is, the cells that satisfy at least one of , , , ) are sea.
Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved times on the grid following the instructions represented by a string of length consisting of L
, R
, U
, and D
. For , the -th character of describes the -th move as follows:
L
indicates a move of one cell to the left. That is, if he is at before the move, he will be at after the move.R
indicates a move of one cell to the right. That is, if he is at before the move, he will be at after the move.U
indicates a move of one cell up. That is, if he is at before the move, he will be at after the move.D
indicates a move of one cell down. That is, if he is at before the move, he will be at after the move.
It is known that all cells along his path (including the cell where he crash-landed and the cell he is currently on) are not sea. Print the number of cells that could be his current position.
Constraints
- , , and are integers.
- is a string of length consisting of
L
,R
,U
, andD
. - is a string of length consisting of
.
and#
. - There is at least one cell that could be Takahashi's current position.
- All cells on the perimeter of the grid are sea.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
6 7 5
LULDR
#######
#...#.#
##...##
#.#...#
#...#.#
#######
Sample Output 1
2
The following two cases are possible, so there are two cells that could be Takahashi's current position: and .
- He crash-landed on cell and moved $(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$.
- He crash-landed on cell and moved $(4, 6) \rightarrow (4, 5) \rightarrow (3, 5) \rightarrow (3, 4) \rightarrow (4, 4) \rightarrow (4, 5)$.
Sample Input 2
13 16 9
ULURDLURD
################
##..##.#..####.#
###.#..#.....#.#
#..##..#####.###
#...#..#......##
###.##.#..#....#
##.#####....##.#
###.###.#.#.#..#
######.....##..#
#...#.#.######.#
##..###..#..#.##
#...#.#.#...#..#
################
Sample Output 2
6
update @ 2024/3/10 01:34:41