
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Cost Path with Left, Right, Bottom and Up moves allowed in C++
Suppose we have a 2D array. Where each cell of which consists number cost which represents a cost to visit through that cell, we have to find a path from top-left cell to bottom-right cell by which total cost consumed is minimum.
So, if the input is like
32 | 10 1 | 66 | 13 | 19 |
11 | 14 | 48 | 15 8 | 7 |
10 1 | 11 14 | 17 5 | 12 | 34 |
89 | 12 5 | 42 | 21 | 14 1 |
10 0 | 33 | 11 2 | 42 | 21 |
then the output will be 340 as (32 + 11 + 14 + 48 + 66 + 13 + 19 + 7 + 34 + 12 + 21 + 42 + 21) = 340
To solve this, we will follow these steps −
- Define cell with x, y coordinate and distance parameter.
- Define an array matrix of size: row x col.
- fill the matrix with inf
- Define an array dx of size: 4 := { - 1, 0, 1, 0}
- Define an array dy of size: 4 := {0, 1, 0, - 1}
- Define one set of cell called st
- insert cell(0, 0, 0) into st
- matrix[0, 0] := grid[0, 0]
- while (not st is empty), do −
- k := first element of st
- delete first element of st from st
- for initialize i := 0, when i < 4, update (increase i by 1), do −
- x := k.x + dx[i]
- y := k.y + dy[i]
- if not isOk(x, y), then −
- Ignore following part, skip to the next iteration
- if matrix[x, y] > matrix[k.x, k.y] + grid[x, y], then −
- if matrix[x, y] is not equal to inf, then −
- find and delete cell(x, y, matrix[x, y]) from st
- matrix[x, y] := matrix[k.x, k.y] + grid[x, y]
- insert cell(x, y, matrix[x, y]) into st
- if matrix[x, y] is not equal to inf, then −
- return matrix[row - 1, col - 1]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define ROW 5 #define COL 5 class cell { public: int x, y; int distance; cell(int x, int y, int distance) : x(x), y(y), distance(distance) {} }; 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); } bool isOk(int i, int j) { return (i >= 0 && i < COL && j >= 0 && j < ROW); } int solve(int grid[ROW][COL], int row, int col) { int matrix[row][col]; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) matrix[i][j] = INT_MAX; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; set<cell> st; st.insert(cell(0, 0, 0)); matrix[0][0] = grid[0][0]; while (!st.empty()) { cell k = *st.begin(); st.erase(st.begin()); for (int i = 0; i < 4; i++) { int x = k.x + dx[i]; int y = k.y + dy[i]; if (!isOk(x, y)) continue; if (matrix[x][y] > matrix[k.x][k.y] + grid[x][y]){ if (matrix[x][y] != INT_MAX) st.erase(st.find(cell(x, y, matrix[x][y]))); matrix[x][y] = matrix[k.x][k.y] + grid[x][y]; st.insert(cell(x, y, matrix[x][y])); } } } return matrix[row - 1][col - 1]; } int main() { int grid[ROW][COL] = { 32, 101, 66, 13, 19, 11, 14, 48, 158, 7, 101, 114, 175, 12, 34, 89, 126, 42, 21, 141, 100, 33, 112, 42, 21 }; cout << solve(grid, ROW, COL); }
Input
{32, 101, 66, 13, 19, 11, 14, 48, 158, 7, 101, 114, 175, 12, 34, 89, 126, 42, 21, 141, 100, 33, 112, 42, 21 };
Output:
340
- Related Articles
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- C Program for Minimum Cost Path
- Minimize Cost with Replacement with other allowed in C++
- Set the left, right, top and bottom padding of an element
- How to detect swipe direction between left/right and up/down in Android?
- Path With Maximum Minimum Value in Python
- Maximum points from top left of matrix to bottom right and return back in C++
- Set bottom-left corner border with CSS
- Minimum flips to make all 1s in left and 0s in right in C++
- How to implement minimum step to one using bottom-up approach using C#?
- How to display the JList items from top to bottom and left to right in Java?
- Minimum Knight Moves in C++
- Min Cost Path
- Set bottom-right corner border with CSS
- Connecting Cities With Minimum Cost in Python\n

Advertisements