Bob and cities

3.4

7 votes
Problem

Bob is living in a city in which houses are arranged in Nxblock.

The city is denoted by strings having characters such that '.' denotes house while '#' denotes forests.

Bob has to pay a certain amount LCost, RCost, UCost, DCost to move 1 step across left, right, up or down respectively.

Bob lives in a house having co-ordinates (Stx , Sty) (1-Based Indexing).

You are given Q tasks contains an integer X each. In each task, you have to find number of unique houses (including his house) can be travelled using the amount X.

INPUT

First line contains two space separated integers N and M, denoting number of rows and columns respectively.

Next N lines contain a string each containing M characters. (Note :- Top left corner will denote {1,1} )

Next line containd four space separated integers denoting LCost, RCost, UCost, DCost respectively.

Next line contains two space separated integers Stx and Sty, denoting position of Bob's house.

Next line contains an integer Q denoting number of tasks.

Next Q lines contain an integer X each, denoting the amount Bob have.

Output

For each task, output a single integer denoting the number of unique houses (including his house) Bob can visit using the amount X.

Constraint

1 <= N, M <= 1000

1 <= Stx <= N 

1 <= Sty <= M

1 <= LCost, RCost, UCost, DCost <= 109

1 <= Q <= 105

0  <= X <= 1018 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

As the starting point is {2, 3}.

In first query Bob has 2 units of money. The total number of unique houses that can be visited are (2,3), (2,2), (2,4). Therefore the answer is .

Similarly we can check for other 2 queries.

Editor Image

?