Given a two dimensional grid, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which total cost incurred is minimum.
Note : It is assumed that negative cost cycles do not exist in input matrix.
This problem is extension of below problem.
Min Cost Path with right and bottom moves allowed.
In previous problem only going right and bottom was allowed but in this problem we are allowed to go bottom, up, right and left i.e. in all 4 direction.
A cost grid is given in below diagram, minimum cost to reach bottom right from top left is 327 (= 31 + 10 + 13 + 47 + 65 + 12 + 18 + 6 + 33 + 11 + 20 + 41 + 20) The chosen least cost path is shown in green.
It is not possible to solve this problem using dynamic programming similar to previous problem because here current state depends not only on right and bottom cells but also on left and upper cells. We solve this problem using dijkstra’s algorithm. Each cell of grid represents a vertex and neighbor cells adjacent vertices. We do not make an explicit graph from these cells instead we will use matrix as it is in our dijkstra’s algorithm.
In below code Dijkstra’ algorithm’s implementation is used. The code implemented below is changed to cope with matrix represented implicit graph. Please also see use of dx and dy arrays in below code, these arrays are taken for simplifying the process of visiting neighbor vertices of each cell.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Find the maximum cost path from the bottom-left corner to the top-right corner
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Maximum sum path in a matrix from top-left to bottom-right
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Maximum non-negative product of a path from top left to bottom right of given Matrix
- Print a matrix in alternate manner (left to right then right to left)
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Maximum points from top left of matrix to bottom right and return back
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Print all palindromic paths from top left to bottom right in a matrix
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Maximize trailing zeros in product from top left to bottom right of given Matrix