
0 0
0 0
2 1
In the above grid, if Bob is present at (0,1), he can't move to (0,0) because the cell at (0,0) is seen by the guard present at (2,0).
0 0
1 0
2 1
In the above grid, Bob can move from (0,1) to (0,0) since this cell is not seen by the guard at (2,0) due to the block present at (1,0).
If all the corner cells are invalid, then return false.
The first line will contain a single integer ‘T’ denoting the number of test cases. Then ‘T’ test cases follow.
The first line of each test case will contain two space-separated integers ‘N’ and ‘M’, denoting the number of rows and number of columns, respectively.
The next ‘N’ lines of each test case will contain ‘M’ space-separated integers denoting the cell values of that corresponding row.
The next line of each test case will contain two space-separated integers denoting the row and column numbers of the destination cell.
It is guaranteed that the integer values in the grid are either 0, 1, or 2.
For each test case, print “YES” if Bob can reach the destination cell, else print “NO”.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N,M <= 10^3
X[i][j] = { 0, 1, 2 } 0 <= i < N, 0 <= j < M
Where X[i][j] denotes the cell value of ith row and jth cell.
It is guaranteed that the sum of N*M over all test cases doesn’t exceed 10^6.
Time Limit: 1 sec.
The approach is similar to the previous one, but here, we will not traverse all four directions for each guard. We will traverse the whole grid four times, and each time we will mark the seen cells by any guard in a particular direction. For example, in the first traversal, we can mark all the cells to the right of all the guards as invalid. In the next traversal, we can mark the left ones—similarly, upper and lower ones in the next two traversals.
After that, we will use multi-source bfs to traverse the grid and return true if the destination cell is visited, otherwise false.
Algorithm: