Recently Ninja has been learning about a new Ninja Technique to cross a field with landmines. The field is in the form of a rectangular matrix of size M x N, having few landmines, which are placed arbitrarily. The landmine gets triggered if a person is any closer than one cell from the position where it is placed. So Ninja must avoid the landmines and any of the four adjacent cells (left, right, above and below), as they are also unsafe.
Initially Ninja is on one side of the field and has to move to the other side. You need to help Ninja apply the new Ninja Technique by providing him with the length of the shortest safe route possible from any cell in the first column to any cell in the last column of the field.
Note that Ninja is only allowed to move only in the direction left, right, above and below i.e., he cannot move diagonally to the next cell.
Note :The length of the path is the number of steps required to reach from the first column of the field to the last column i.e., one less than the number of cells in the path.
For Example :
Consider the field of size 4*4, shown below. The cells containing landmine are marked with 0 and red colour. The cells near the landmine which are unsafe are marked with a light red colour.
The shortest safe route for Ninja, starting from any cell in the first column to any cell in the last column of the field is marked with green colour. The length of the path is 3.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains two space-separated integers ‘M’ and ‘N’ denoting the number of rows and columns, respectively, in the field matrix.
The next 'M' lines of the input contain 'N' space-separated integers - 0 or 1, where 0 denotes the presence of landmine at that cell and 1 denotes the absence of landmine.
Output Format :
For each test case, print the length of the shortest safe route possible from any cell in the first column to any cell in the last column of the field. In case no such path exists, print -1.
Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just return the length of the shortest path.
1 <= T <= 10
1 <= N, M <= 1000
0 <= Field[i][j] <= 1
Time Limit: 1 sec
2
4 4
1 1 1 1
1 1 1 1
1 1 0 1
0 1 1 1
6 5
1 1 1 0 1
1 1 1 1 0
0 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 0 1 1 1
3
7
For the first test case, refer to the example explained above.
For the second test case, we have a field of size 5*6, as shown below. The cells containing landmine are marked with 0 and red colour. The cells near the landmine which are unsafe are marked with a light red colour.
The shortest safe route for Ninja, starting from any cell in the first column to any cell in the last column of the field is marked with green colour. The length of the path is 7.
2
1 1
1
2 2
1 0
1 1
0
-1
Try generating all the possible paths from the first column of the field to the last column and store the length of the shortest path.
O(3^(M*N)), where ‘M’ represents the number of rows in the field and ‘N’ represents the number of columns in the field.
Marking the unsafe nodes as 0 requires O(M*N) time. Also, in the worst case, we make three recursive calls for every cell in the field. As there are M*N cells, hence it requires O(3^(M*N)). So, the overall time complexity is O(M*N + 3^(M*N)) = O(3^(M*N))
O(M*N), where ‘M’ represents the number of rows in the field and ‘N’ represents the number of columns in the field.
Extra space is required for the recursion stack. In the worst case, the depth of the recursion stack can be of order O(M*N). Hence, the overall space complexity is O(M*N).