Ninja is stuck in a maze with empty spaces and walls. Ninja can go through empty spaces by running up, down, left or right, but he will continue running in the same direction until hitting a wall. When he stops, he could choose the next direction.
Given Ninja's start position, the destination and the maze, find the shortest distance for Ninja to stop at the destination. The distance is defined by the number of empty spaces traveled by Ninja from the start position (excluded) to the destination (included). If Ninja cannot stop at the destination, return -1.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
Note :Both Ninja’s starting cell and the destination cell exist on an empty space, and they will not be at the same position initially.
The given maze does not contain borders (like the red rectangle in the sample pictures), but you could assume the borders of the maze are all walls.
Example :
N = 3, M = 3
Maze = [ [ 0, 0, 0 ], [ 0, 1, 0 ], [ 1, 0, 0] ]
Start = [ 0, 0 ]
Destination = [ 2, 1 ]
Explanation :
Ninja can start from (0,0) and take the right direction and run till (0,2). Then he turns direction to down and runs till (2,2). Then Ninja turns direction to the left and runs till (2,1).
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains integers ‘N’ and ‘M’ representing the number of rows and columns of the maze.
The next ‘N’ line contains ‘M’ integers representing the cells of the maze. 1 represents a wall and 0 represents the empty space.
The next line contains two integers representing the coordinates of the starting position of Ninja.
The next line contains the destination coordinates.
Output format :
For each test case, output an integer denoting the shortest distance required to reach from the starting position to the destination. If it is impossible to reach the destination, output -1.
Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
Time Limit : 1 sec
1
5 5
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
0 4
4 4
12
For test case 1 we have,
The starting position is (0,4) and the destination is (4,4).
One of the shortest ways is : left -> down -> left -> down -> right -> down -> right.
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
So, we output 12.
2
3 4
1 0 1 0
0 0 0 1
0 0 1 1
2 1
1 2
3 5
0 0 0 1 0
0 1 1 1 1
1 1 0 0 0
2 4
0 0
4
-1
Think of the BFS approach.
Approach :
Algorithm :
O( N*M*Max(N,M) ) , where ‘N’ is the number of rows and ‘M’ is the number of columns in the maze.
We are checking each cell of the maze until we reach the destination and for each cell we can travel a maximum of (M,N) cells , so the overall time complexity is O( N*M*Max(N,M) ).
O(N*M)
We are maintaining a 2-D array to mark the visited cells. Hence, the overall Space Complexity is O(N*M).