#abc341c. C - Takahashi Gets Lost

C - Takahashi Gets Lost

Score: 250250 points

问题描述

存在一个包含 HH 行和 WW 列的网格。

网格中的每个单元格是 陆地海洋,由长度为 WWHH 个字符串 S1,S2,,SHS_1, S_2, \ldots, S_H 表示。令 (i,j)(i, j) 表示从上到下的第 ii 行、从左到右的第 jj 列的单元格,如果 SiS_i 的第 jj 个字符是 ., 那么 (i,j)(i, j) 是陆地;若字符是 #,则 (i,j)(i, j) 是海洋。

约束条件保证了网格边缘的所有单元格(即满足至少一个条件:i=1i = 1, i=Hi = H, j=1j = 1, j=Wj = W)都是海洋。

高桥的宇宙飞船在网格中的某个单元格迫降。之后,他按照由长度为 NN 的字符串 TT(包含 LRUD 字符)表示的指令,在网格上移动了 NN 次。对于 i=1,2,,Ni = 1, 2, \ldots, NTT 中的第 ii 个字符如下描述第 ii 次移动:

  • L 表示向左移动一格。即,如果他在移动前位于 (i,j)(i, j),那么移动后将位于 (i,j1)(i, j-1)
  • R 表示向右移动一格。即,如果他在移动前位于 (i,j)(i, j),那么移动后将位于 (i,j+1)(i, j+1)
  • U 表示向上移动一格。即,如果他在移动前位于 (i,j)(i, j),那么移动后将位于 (i1,j)(i-1, j)
  • D 表示向下移动一格。即,如果他在移动前位于 (i,j)(i, j),那么移动后将位于 (i+1,j)(i+1, j)

已知沿他的路径的所有单元格(包括他迫降的单元格以及他当前所在的单元格)都不是海洋。输出可能成为他当前位置的单元格数量。

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

Problem Statement

There is a grid with HH rows and WW columns.

Each cell of the grid is land or sea, which is represented by HH strings S1,S2,,SHS_1, S_2, \ldots, S_H of length WW. Let (i,j)(i, j) denote the cell at the ii-th row from the top and jj-th column from the left, and (i,j)(i, j) is land if the jj-th character of SiS_i is ., and (i,j)(i, j) is sea if the character is #.

The constraints guarantee that all cells on the perimeter of the grid (that is, the cells (i,j)(i, j) that satisfy at least one of i=1i = 1, i=Hi = H, j=1j = 1, j=Wj = W) are sea.

Takahashi's spaceship has crash-landed on a cell in the grid. Afterward, he moved NN times on the grid following the instructions represented by a string TT of length NN consisting of L, R, U, and D. For i=1,2,,Ni = 1, 2, \ldots, N, the ii-th character of TT describes the ii-th move as follows:

  • L indicates a move of one cell to the left. That is, if he is at (i,j)(i, j) before the move, he will be at (i,j1)(i, j-1) after the move.
  • R indicates a move of one cell to the right. That is, if he is at (i,j)(i, j) before the move, he will be at (i,j+1)(i, j+1) after the move.
  • U indicates a move of one cell up. That is, if he is at (i,j)(i, j) before the move, he will be at (i1,j)(i-1, j) after the move.
  • D indicates a move of one cell down. That is, if he is at (i,j)(i, j) before the move, he will be at (i+1,j)(i+1, j) 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

  • HH, WW, and NN are integers.
  • 3H,W5003 \leq H, W \leq 500
  • 1N5001 \leq N \leq 500
  • TT is a string of length NN consisting of L, R, U, and D.
  • SiS_i is a string of length WW 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:

HH WW NN

TT

S1S_1

S2S_2

\vdots

SHS_H

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: (3,4)(3, 4) and (4,5)(4, 5).

  • He crash-landed on cell (3,5)(3, 5) and moved $(3, 5) \rightarrow (3, 4) \rightarrow (2, 4) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$.
  • He crash-landed on cell (4,6)(4, 6) 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