-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum cost path.cpp
56 lines (51 loc) · 1.3 KB
/
minimum cost path.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
55
56
/* Dynamic Programming implementation of MCP problem */
#include<stdio.h>
#include<limits.h>
#define R 3
#define C 3
// apply a greedy approach
int min(int x, int y, int z);
int minCost(int cost[R][C], int m, int n)
{
int result[R][C]={0};
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i==0&&j==0)
{
result[i][j]=cost[i][j]; // no way to go
}
else if(i==0)
{
result[i][j]=cost[i][j]+result[i][j-1]; // can only move right
}
else if(j==0)
{
result[i][j]=cost[i][j]+result[i-1][j]; // can only move down
}
else
{
result[i][j]=cost[i][j]+min(result[i][j-1],result[i-1][j],result[i-1][j-1]); //minimum of the three path options
}
}
}
return result[m-1][n-1];
}
/* A utility function that returns minimum of 3 integers */
int min(int x, int y, int z)
{
if (x < y)
return (x < z)? x : z;
else
return (y < z)? y : z;
}
/* Driver program to test above functions */
int main()
{
int cost[R][C] = { {1, 2, 3},
{4, 8, 2},
{1, 5, 3} };
printf(" %d ", minCost(cost, 3, 3));
return 0;
}