# Minimum Cost Path with Left, Right, Bottom and Up moves allowed

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.

Examples:

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.

`// C++ program to get least cost path in a grid from ` `// top-left to bottom-right ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ROW 5 ` `#define COL 5 ` ` ` `// structure for information of each cell ` `struct` `cell ` `{ ` ` ` `int` `x, y; ` ` ` `int` `distance; ` ` ` `cell(` `int` `x, ` `int` `y, ` `int` `distance) : ` ` ` `x(x), y(y), distance(distance) {} ` `}; ` ` ` `// Utility method for comparing two cells ` `bool` `operator<(` `const` `cell& a, ` `const` `cell& b) ` `{ ` ` ` `if` `(a.distance == b.distance) ` ` ` `{ ` ` ` `if` `(a.x != b.x) ` ` ` `return` `(a.x < b.x); ` ` ` `else` ` ` `return` `(a.y < b.y); ` ` ` `} ` ` ` `return` `(a.distance < b.distance); ` `} ` ` ` `// Utility method to check whether a point is ` `// inside the grid or not ` `bool` `isInsideGrid(` `int` `i, ` `int` `j) ` `{ ` ` ` `return` `(i >= 0 && i < COL && j >= 0 && j < ROW); ` `} ` ` ` `// Method returns minimum cost to reach bottom ` `// right from top left ` `int` `shortest(` `int` `grid[ROW][COL], ` `int` `row, ` `int` `col) ` `{ ` ` ` `int` `dis[row][col]; ` ` ` ` ` `// initializing distance array by INT_MAX ` ` ` `for` `(` `int` `i = 0; i < row; i++) ` ` ` `for` `(` `int` `j = 0; j < col; j++) ` ` ` `dis[i][j] = INT_MAX; ` ` ` ` ` `// direction arrays for simplification of getting ` ` ` `// neighbour ` ` ` `int` `dx[] = {-1, 0, 1, 0}; ` ` ` `int` `dy[] = {0, 1, 0, -1}; ` ` ` ` ` `set<cell> st; ` ` ` ` ` `// insert (0, 0) cell with 0 distance ` ` ` `st.insert(cell(0, 0, 0)); ` ` ` ` ` `// initialize distance of (0, 0) with its grid value ` ` ` `dis[0][0] = grid[0][0]; ` ` ` ` ` `// loop for standard dijkstra's algorithm ` ` ` `while` `(!st.empty()) ` ` ` `{ ` ` ` `// get the cell with minimum distance and delete ` ` ` `// it from the set ` ` ` `cell k = *st.begin(); ` ` ` `st.erase(st.begin()); ` ` ` ` ` `// looping through all neighbours ` ` ` `for` `(` `int` `i = 0; i < 4; i++) ` ` ` `{ ` ` ` `int` `x = k.x + dx[i]; ` ` ` `int` `y = k.y + dy[i]; ` ` ` ` ` `// if not inside boundary, ignore them ` ` ` `if` `(!isInsideGrid(x, y)) ` ` ` `continue` `; ` ` ` ` ` `// If distance from current cell is smaller, then ` ` ` `// update distance of neighbour cell ` ` ` `if` `(dis[x][y] > dis[k.x][k.y] + grid[x][y]) ` ` ` `{ ` ` ` `// If cell is already there in set, then ` ` ` `// remove its previous entry ` ` ` `if` `(dis[x][y] != INT_MAX) ` ` ` `st.erase(st.find(cell(x, y, dis[x][y]))); ` ` ` ` ` `// update the distance and insert new updated ` ` ` `// cell in set ` ` ` `dis[x][y] = dis[k.x][k.y] + grid[x][y]; ` ` ` `st.insert(cell(x, y, dis[x][y])); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// uncomment below code to print distance ` ` ` `// of each cell from (0, 0) ` ` ` `/* ` ` ` `for (int i = 0; i < row; i++, cout << endl) ` ` ` `for (int j = 0; j < col; j++) ` ` ` `cout << dis[i][j] << " "; ` ` ` `*/` ` ` `// dis[row - 1][col - 1] will represent final ` ` ` `// distance of bottom right cell from top left cell ` ` ` `return` `dis[row - 1][col - 1]; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `grid[ROW][COL] = ` ` ` `{ ` ` ` `31, 100, 65, 12, 18, ` ` ` `10, 13, 47, 157, 6, ` ` ` `100, 113, 174, 11, 33, ` ` ` `88, 124, 41, 20, 140, ` ` ` `99, 32, 111, 41, 20 ` ` ` `}; ` ` ` ` ` `cout << shortest(grid, ROW, COL) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

327

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 contribute@geeksforgeeks.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.

## Recommended Posts:

- 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
- 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 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
- Print all palindromic paths from top left to bottom right in a matrix
- Maximum points from top left of matrix to bottom right and return back
- Minimize Cost with Replacement with other allowed
- Maximum sum path in a matrix from top to bottom
- Maximum sum path in a matrix from top to bottom and back