Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements