Given a rectangular field with few sensors present, cross it by taking the shortest safe route without activating the sensors.

The rectangular field is in the form of an M × N matrix, and we need to find the shortest path from any cell in the first column to any cell in the last column of the matrix. The sensors are marked by the value 0 in the matrix, and all its eight adjacent cells can also activate the sensors. The path can only be constructed out of cells having value 1, and at any given moment, we can only move one step in one of the four directions. The valid moves are:

Go Up: (x, y) ——> (x – 1, y)
Go Left: (x, y) ——> (x, y – 1)
Go Down: (x, y) ——> (x + 1, y)
Go Right: (x, y) ——> (x, y + 1)

For example, consider the following matrix:

 
Shortest Path – Step 1

 
The shortest safe path has a length of 11, and the route is marked in green.

Shortest Path – Step 2
 

Practice this problem

 
The idea is to use Breadth–first search (BFS) since it is the shortest path problem. Following is the complete algorithm:

  1. Create a queue and enqueue every safe cell of the first column and set their distance as 0 from the source (itself). Also, mark them as visited as we enqueue them.
  2. Loop till queue is empty
    1. Dequeue the front node.
    2. If the popped node is the destination node (last column), return its distance.
    3. Otherwise, for each of four adjacent cells of the current cell, enqueue each valid cell with +1 distance and mark them as visited.
  3. If all the queue nodes are processed, and the destination is not reached, then return false.

 
We can find all the possible locations we can move to from the given location by using the array that stores the relative position of movement from any location. For example, if the current location is (x, y), we can move to (x + row[k], y + col[k]) for 0 <= k < 4 using the following array:

row[] = { -1, 0, 0, 1 }
col[] = { 0, -1, 1, 0 }
 
So, from position (x, y), we can move to:
 
(x – 1, y)
(x, y – 1)
(x, y + 1)
(x + 1, y)

Note that in BFS, all cells having the shortest path as 1 are visited first, followed by their adjacent cells having the shortest path as 1 + 1 = 2 and so on… so if we reach any node in BFS, its shortest path is one more than the shortest path of the parent. So, the first occurrence of the destination cell gives us the result, and we can stop our search there. The shortest path cannot possibly exist from some other cell for which we haven’t reached the given node yet. If any such path was possible, we would have already explored it.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The shortest safe path has a length of 11

Java


Download  Run Code

Output:

The shortest safe path has a length of 11

Python


Download  Run Code

Output:

The shortest safe path has a length of 11

The time complexity of the proposed solution is O(M × N) and requires O(M × N) extra space, where M and N are dimensions of the matrix.

 
Exercise: Extend the solution to print the shortest path.