Bombing
Time limit2sMemory limit512 MB
Given a fixed N by N bomb pattern and a walk of L moves, count grid cells damaged at least K times by the repeated bombings.
- Level
Medium7 of 10
- Topics
- Prefix sum, Implementation, Matrix, Brute force
- Solved
- No attempts yet
Problem
JAG land is a country represented as an grid. Its top-left cell is and its bottom-right cell is .
Suddenly, a bomber invaded JAG land and dropped bombs on the country. Its bombing pattern is always fixed and is represented by an grid. Each symbol in the bombing pattern is either 'X' (bomb) or '.' (empty).
Suppose the bomber is at in the land and drops a bomb. The cell is damaged if the symbol in the -th row and the -th column of the bombing pattern is 'X' ().
Initially, the bomber arrived at in JAG land. The bomber repeated moving in one of 4 directions and then dropping a bomb exactly times. During this attack, the values of the bomber's coordinates were between 1 and , inclusive, whenever it dropped bombs. Finally, the bomber left the country.
The bomber's movement pattern is given as characters. The -th character corresponds to the -th move and the meaning of each character is as follows.
'U' is up, 'D' is down, 'L' is left, and 'R' is right.
Your task is to write a program that analyzes the damage situation in JAG land. To investigate the damage overview in the land, calculate the number of cells that were damaged by the bomber at least times.
Input
The first line of the input contains four integers , , , and (, ). The following lines represent the bombing pattern. is a string of length . Each character of is either 'X' or '.'. The last line is the movement pattern. is a string of length consisting of 'U', 'D', 'L', or 'R'. It is guaranteed that the values of the bomber's coordinates are between 1 and , inclusive, whenever it drops bombs in the country.
Output
Print the number of cells that were damaged by the bomber at least times.