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.
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 using C++ STL 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Print all paths from top left to bottom right in a matrix with four moves allowed
- 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
- Find the maximum cost path from the bottom-left corner to the top-right corner
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum odd cost path in a matrix
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum cost for acquiring all coins with k extra coins allowed with every coin
- Minimum cost path from source node to destination node via an intermediate node
- Count all possible paths from top left to bottom right of a mXn matrix
- Print all possible paths from top left to bottom right of a mXn matrix
- Minimize Cost with Replacement with other allowed
Improved By : Akanksha_Rai