
Arr[i][j] = 0 denotes that the house is empty
Arr[i][j] = 1 denotes that the house has exactly one gold bar
Arr[i][j] = 2 denotes that the house is secured by the guards
Ninja may visit a house multiple times, but he will be able to collect the gold bar only once from that house.
If ‘Arr’ consists of 4 rows and 5 columns and is denoted as:
0 1 1 2 0
2 2 1 1 2
0 0 1 2 0
0 1 1 0 0
And ninja’s home is located at (2, 1).
The minimum number of moves required to complete the task is equal to 9, because:
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (1, 3) -> (1, 2) -> (2, 2) -> (3, 2) -> (3, 1) -> (2, 1) is the most optimal route to complete the task.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains two integers ‘M’ and ‘N’ denoting the number of rows and columns.
The following M rows each contain N integers denoting the status of each house.
The last line of each test case contains two integers ‘X’ and ‘Y’ denoting the x-coordinate and y-coordinate of Ninja’s house (his final destination).
For each test case, print a single integer denoting the minimum number of steps required.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= M, N <= 20
Arr[i][j] = {0, 1, 2}
1 <= Number of houses with gold <= 7
0 <= X < M , 0 <= Y < N
Time limit: 1 sec
There can be at max eight houses that can contain gold in them. We can easily number these houses starting from 0, now we can use “MASK” as our bitmask. We will initially set all the bits equal to 0 in “MASK”, now while applying breadth-first search if we visit the ith house containing gold then we will set the ith bit in “MASK” as 1.
We will store the minimum distance to reach the position (x,y) in distance[ { x, y, MASK } ], and we will finally return the value of distance[ { X, Y, (111...1)2 } ].
The steps are as follows :