-
Notifications
You must be signed in to change notification settings - Fork 45
/
minimumcost.cpp
54 lines (45 loc) · 1.17 KB
/
minimumcost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
// Naive recursive function to find the minimum cost to reach
// cell (m, n) from cell (0, 0)
int findMinCost(vector<vector<int>> const &cost, int m, int n)
{
// base case
if (n == 0 || m == 0) {
return INT_MAX;
}
// if we are in the first cell (0, 0)
if (m == 1 && n == 1) {
return cost[0][0];
}
// include the current cell's cost in the path and recur to find the minimum
// of the path from the adjacent left cell and adjacent top cell.
return min (findMinCost(cost, m - 1, n), findMinCost(cost, m, n - 1))
+ cost[m - 1][n - 1];
}
int findMinCost(vector<vector<int>> const &cost)
{
// base case
if (cost.size() == 0) {
return 0;
}
// `M × N` matrix
int M = cost.size();
int N = cost[0].size();
return findMinCost(cost, M, N);
}
int main()
{
vector<vector<int>> cost =
{
{ 4, 7, 8, 6, 4 },
{ 6, 7, 3, 9, 2 },
{ 3, 8, 1, 2, 4 },
{ 7, 1, 7, 3, 7 },
{ 2, 9, 8, 9, 3 }
};
cout << "The minimum cost is " << findMinCost(cost);
return 0;
}