Anurag and his Endoscope

4.5

36 votes
Hard
Problem

Anurag, a CSE student from MANIT developed an endoscope to explore inner part of ruined water pipes. It is possible explore the inner part of the pipes putting the endoscope into a certain part of the pipe. The endoscope can be moved in the pipes only. Meanwhile, when the pipes are connected to each other, if the length of the endoscope is long enough to explore, it is able to inspect the connected pipe. However, we cannot observe every pipe because the length of the endoscope is limited.

When the map of the ground water pipe, the location where the endoscope to out in, and the length of the endoscope is given, calculate the number of pipe which are available to explore. Length of endoscope means the range upto which endoscope can explore. There are seven kind of pipes, and description for each pipe are shown below

enter image description here - 1 enter image description here - 2 enter image description here - 3 enter image description here - 4 enter image description here - 5 enter image description here - 6 enter image description here - 7

Fig 1 Shows an example of a map of ground water pipes. In this case, the height of the map is 5, and the width is 6 If a certain point where the endoscope to put in is given as (2,1), it means the vertical location will be 2, and the horizontal location will be 1, shown as a red highlighted pipe in fig 2. If the length of the endoscope is 1, we can explore only one water pipe which is at the intersection point If the length of the endoscope is 2, we can explore 3 water pipes including a blue highlighted pipe and a red marked pipe with the endoscope Fig-3 . If the length of the endoscope is 3, we can explore 5 water pipes total Fig-4

enter image description here ::Fig-1

enter image description here ::Fig-2

enter image description here ::Fig-3

enter image description here ::Fig-4

INPUT:

In the first line, T, the number of total test cases is given. From the second line, T test cases are given. In the first line of each test case, N, the height of the map of the ground water pipes, M, the width, R, the vertical location of the water pipe where to put in the endoscope, C, the horizontal location of it, and the length of the endoscope L are given. In the following N lines information of the map of ground water pipe is given. Each line has M numbers. Each number (from 1 to 7) means the type of water pipe for that point. 0 means there is no water pipe buried in that place.

OUTPUT:

Print the respective answer for T test cases in total for T lines. The answer is the number of water pipes which is available to observe using the endoscope.

CONSTRAINTS:

1≤ T ≤100

1≤ N, M ≤1000

0≤ X < N

0≤ Y < M

1≤ L ≤ 1000000

Sum of NxM over all the test cases does not exceed 1000000.

Time Limit: 2
Memory Limit: 256
Source Limit:
Editor Image

?