Bob and cities
Practice
3.4 (7 votes)
Algorithms
Binary search
Breadth first search
Graphs
Implementation
Medium
Problem
68% Success 3722 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Bob is living in a city in which houses are arranged in NxM block.

The city is denoted by N strings having M 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

Sample Input
3 4
..#.
#...
..#.
1 2 3 4
2 3
3
2
5
10
Sample Output
3
7
9
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 3 .

Similarly we can check for other 2 queries.

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
GraphsAlgorithmsBreadth-first search
Points:10
166 votes
Tags:
ReadyBasic ProgrammingApprovedVery EasyVery-Easy
Points:30
5 votes
Tags:
ImplementationGraphsAlgorithmsBrute ForceBreadth-first search
Editorial

Login to unlock the editorial