Trampler
InterviewTime limit2sMemory limit256 MB
For each starting cell of an H by W grid, follow a fixed move string and output the sum of visited cell times, or 0 if the path leaves the field.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Brute force
- Solved
- No attempts yet
Problem
Trampler the Jackrabbit loves trampling fields. He has just found a good new field. As seen on the map, the field is a rectangle cells high and cells wide. When Trampler gets into a cell, he tramples it down to the last twig. It takes him a while to do that, and that time is defined for each specific cell.
Trampler is not sure where to begin, but he has already figured out the sequence of moves. He scribbled it down on a piece of paper, marking each elemental move with a letter.

There are different kinds of moves:
- letter
'U': move from the current field cell to the cell directly adjacent above; - letter
'R': move to the adjacent cell on the right; - letter
'L': move to the adjacent cell on the left; - letter
'D': move to the adjacent cell below.
For each cell, Trampler needs to know the time necessary to implement his plan, should he begin his trampling route from that cell. He will spend some time in the initial cell and in each consecutive cell, even if that cell has already been trampled.
He is not planning to leave the field until he is done. If starting at a certain cell means having to leave the field eventually, this cell is no good for a starting point. Trampler wants such cells marked with 0.
Input
The first line of the input file defines an integer , the number of test cases (). It is followed by blocks.
The first line of a block contains two integers and , the height and width of the field, respectively ().
The second line of the block contains a non-empty sequence of moves. Each move is represented by one of the symbols 'L', 'R', 'U', 'D'. The number of moves is less than or equal to .
Each of the following lines contains integers: the time the jackrabbit spends in a specific field cell. These numbers fall in the range from through .
The total number of cells in all test cases is less than or equal to .
Output
For each test case, print lines of integers: the answers for each of the field cells.
Notes
The illustration in the problem statement shows the field from the first example and the directions of moves.