


Ninja went to an amusement park and visited a maze. Now, he is stuck in the maze. He can go in any direction(Up, Down, Left, or Right) from this point, but he cannot change his direction of motion until he comes across a wall in its path. Once he stops, he can choose his new direction.
Now, you are given Ninja’s starting point, the destination he wants to reach, and the maze in the form of a 2D matrix. You need to find out if Ninja can reach the destination from the starting point or not.
The maze is represented by a 2D array. ‘1’ means Ninja came across a wall and he needs to stop. ‘0’ means that it is an empty space and he can keep moving.
The coordinates of starting point and destination are represented by row and column numbers.
For ExampleGiven maze: {[0, 0, 1], [1, 0, 0], [1, 0, 0]}
Starting point: [2, 2]
Destination: [0, 0]
For the above example maze will look like this:

So, we can see there are 2 ways for Ninja to reach destination(D), from the starting point(SP). They are: [left -> up -> left] and [up -> left -> up -> left].
So, you need to print true.
The first line contains an integer ‘T’ which denotes the number of test cases.
The first line of each test case contains two space-separated integers ‘M’ and ‘N’ denoting the dimensions of the maze.
The next ‘M’ lines contain ‘N’ space-separated integers each denoting the maze.
The next line of each test case contains two space-separated integers ‘startX’ and ‘startY’ denoting the coordinates of the starting point.
The last line of each test case contains two space-separated integers ‘destX’ and ‘destY’ denoting the coordinates of the destination.
Output Format
For each test case, print a single line containing a single string containing ‘True’ if it is possible to reach the destination from the starting point and ‘False’ if it is not possible.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <=T <= 50
1 <= M,N <= 100
0 <= MAZE[ i ] [ j ] <= 1
Where MAZE[ i ][ j ] is the value of the maze. The starting point and destination will always lie in the maze and will be in empty spaces.
Time limit: 1 sec.
1
3 3
0 0 1
1 0 0
1 0 0
2 2
0 0
True
Test Case 1:
The maze given in the test case will look like this:

So, we can see there are 2 ways for Ninja to reach destination(D), from the starting point(SP). They are: [left -> up -> left] and [up -> left -> up -> left].
So, you need to print true.
2
3 3
0 0 1
1 0 0
1 0 1
0 0
1 2
3 3
1 0 1
0 0 0
1 0 1
1 0
2 1
False
False
Recursively check for every cell.
We will store all the four directions { (0,1) (0,-1) ( 1,0) ( -1,0 ) )} in 2-d array.
There will be many cases in which we will stop at a cell that is already explored. In order to not explore that cell again, we will maintain an extra array ‘visited’.
Algorithm
O(M * N), where ‘M’ and ‘N’ are the dimensions of the maze.
Every cell of the maze will be visited only once. So the time complexity will be O(M * N).
O(M * N), where ‘M’ and ‘N’ are the dimensions of the maze.
As we are recursively solving the problem at every stopping point. The maximum number of stopping points will be equal to the size of the maze i.e. M * N. Therefore space complexity would be O(M * N).