Given an N × N matrix of positive integers, find the shortest path from the first cell of the matrix to its last cell that satisfies given constraints.

We are allowed to move exactly k steps from any cell in the matrix where k is the cell’s value, i.e., from a cell (i, j) having value k in a matrix M, we can move to (i+k, j), (i-k, j), (i, j+k), or (i, j-k). The diagonal moves are not allowed.

For example,

Input:
 
[ 7  1  3  5  3  6  1  1  7  5 ]
[ 2  3  6  1  1  6  6  6  1  2 ]
[ 6  1  7  2  1  4  7  6  6  2 ]
[ 6  6  7  1  3  3  5  1  3  4 ]
[ 5  5  6  1  5  4  6  1  7  4 ]
[ 3  5  5  2  7  5  3  4  3  6 ]
[ 4  1  4  3  6  4  5  3  2  6 ]
[ 4  4  1  7  4  3  3  1  4  2 ]
[ 4  4  5  1  5  2  3  5  3  5 ]
[ 3  6  3  5  2  2  6  4  2  1 ]
 
Output:
 
The shortest path length is 6
The shortest path is (0, 0) (0, 7) (0, 6) (1, 6) (7, 6) (7, 9) (9, 9)
 
 
Input:
 
[ 4  4  6  5  5  1  1  1  7  4 ]
[ 3  6  2  4  6  5  7  2  6  6 ]
[ 1  3  6  1  1  1  7  1  4  5 ]
[ 7  5  6  3  1  3  3  1  1  7 ]
[ 3  4  6  4  7  2  6  5  4  4 ]
[ 3  2  5  1  2  5  1  2  3  4 ]
[ 4  2  2  2  5  2  3  7  7  3 ]
[ 7  2  4  3  5  2  2  3  6  3 ]
[ 5  1  4  2  6  4  6  7  3  7 ]
[ 1  4  1  7  5  3  6  5  3  4 ]
 
Output:
 
The shortest path length is 6
The shortest path is (0, 0) (0, 4) (5, 4) (5, 2) (5, 7) (5, 9) (9, 9)

Practice this problem

We have already discussed a backtracking solution in the previous post. The time complexity of the backtracking solution would be higher since all paths need to be traveled until the destination is reached. However, since it is the shortest path problem, Breadth–first search (BFS) would be an ideal choice. This post proposes the BFS solution.

Following is the complete algorithm:

  1. Create an empty queue and enqueue the source cell having a distance 0 from source (itself) and mark it as visited.
  2. Loop till queue is empty

    1. Dequeue the front node.
    2. If the popped node is the destination node, return its distance.
    3. Otherwise, for each of four adjacent cells of the current cell, enqueue each of the valid cells 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.

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.

 
Note that 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)

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

C++


Download  Run Code

Output:

The shortest path is (0, 0) (0, 4) (5, 4) (5, 2) (5, 7) (5, 9) (9, 9)

Java


Download  Run Code

Output:

The shortest path is [(0, 0), (0, 4), (5, 4), (5, 2), (5, 7), (5, 9), (9, 9)]

Python


Download  Run Code

Output:

The shortest path is [(0, 0), (0, 4), (5, 4), (5, 2), (5, 7), (5, 9), (9, 9)]

In the above program, each node in the queue takes extra space as we are storing path information along with it. The space complexity can be improved if we are asked only to find the shortest distance from the source to the destination. The implementation can be seen below in C++, Java, and Python:

C++


Download  Run Code

Output:

The shortest path length is 6

Java


Download  Run Code

Output:

The shortest path length is 6

Python


Download  Run Code

Output:

The shortest path length is 6