UOFTAC - Foxhole

no tags 

Everyone knows that Foxen love digging holes. You've been observing one Fox in particular, who's preparing to burrow underground in search of treasures. When viewed from the side, as a cross-section, his digging site can be represented as a grid of cells, $H$ ($1 \leq H \leq 100$) deep and $W$ ($1 \leq W \leq 100$) across. Every cell contains either dirt (represented by "D"), stone ("S"), empty space ("E"), or a treasure ("T"). The surface can also be traversed, effectively giving the grid an additional row of empty cells above the topmost row.

The Fox starts immediately above the top-left cell, which is guaranteed to not be empty. It has a set of $N$ ($1 \leq N \leq 1000$) actions in mind before it starts its dig, which it will execute in order. Each action consists of moving either left (represented by "L"), right ("R"), or down ("D") by one cell. If the cell that the Fox would move to contains stone, or is beyond the boundaries of the grid in any direction, it will skip that action. If it enters a cell with a previously-uncollected treasure, it will collect it, leaving the cell empty. If the cell immediately below the Fox is ever empty, it will fall down until this is no longer the case. Note that collecting treasure occurs before falling, and that the Fox stops falling if it hits the bottom of the grid.

There are $T$ ($1 \leq T \leq 20$) scenarios as described above. For each one, you'd like to determine how many treasures the Fox will collect throughout the course of its dig.

Input

First line: 1 integer, $T$

For each scenario:

First line: 3 integers, $H$, $W$, and $N$

Next $H$ lines: $W$ characters, representing the $i$th row of the grid, for $i = 1..H$

Next $N$ lines: 1 character, representing the $i$th action, for $i = 1..N$

Output

For each scenario:

1 integer, the number of treasures collected in total.

Example

Input:
2
2 3 4
DDD
TES
R
D
R
L
3 2 6
TE
TE
ET
R
R
L
R
L
R
Output: 1
2

Explanation of Sample:

In the first scenario, the Fox moves right along the surface, then digs down to the first row. As the cell below it is empty, it immediately falls to the 2nd row. It ignores its next action, as the cell to its right is filled with stone, and finally moves left to claim a treasure.

In the second scenario, it moves to the right and promptly falls all the way down to the 2nd row. Because the Fox is already in the rightmost column, it ignores the action to move right. It then moves left, collects the treasure there, and then falls to the bottom row. Finally, it moves back and forth between the bottom-left and bottom-right cells twice - however, it only collects the treasure in the latter cell the first time.


hide comments
havish chennamaraj: 2013-05-30 16:18:49

What would the output be for the following test case?
1
5 1 1
T
E
E
E
E
D
Thank you..:)

Last edit: 2013-05-30 18:03:21
Eduardo Nunes: 2013-05-30 15:12:20

really nice and simple one :-D
@vishal chaudhary, for both cases answer is 1 ;-) just code exactly what description says =)

vishal chaudhary: 2013-05-29 10:13:01

if the fox encounters a stone or dirt would he stops falling ..?what would be the output of this case
4 4 4
TDES
DDED
TDES
SSDS
R
R
L
L
also if D in last row replaced by S ..what be the output..?

Last edit: 2013-05-29 10:13:22
Himanshu Srivastava: 2013-05-21 08:58:20

@shiva thanks..i was getting the same dnt knw why i m getting WA may be some case is missing :(

shiva_hellgeek: 2013-05-21 08:53:26

@himanshu shrivastava: ans is 1

Himanshu Srivastava: 2013-05-21 07:15:28

if value of H and W is 5 and 1.
T
T
T
E
S
and there are 3 operations given as
R
L
D
what would be the answer?

shiva_hellgeek: 2013-05-20 18:35:29

Really a nice problem
Read each and every word of the problem very carefully. Not reading it properly lead to 6 WA answers for me.
:p

Aditya Bahuguna: 2013-05-20 10:44:29

Enjoyed solving it!!
Nice Problem...Crystal Clear Explanation..

RE: Thanks!

Last edit: 2013-05-20 14:38:19
Haijun Deng: 2013-05-20 04:26:33

2
3 3 4
DDD
TES
should be
2
2 3 4
DDD
TES
Right?

RE: Ah, right - it's been fixed.

Last edit: 2013-05-20 04:26:46

Added by:SourSpinach
Date:2013-05-17
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own problem, used in the 2012 UofT ACM Tryouts