-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinCost.cpp
57 lines (49 loc) · 890 Bytes
/
MinCost.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
57
// MinCost
#include <iostream>
using namespace std;
int MinCost(int i,int j,int cost[][4],int dp[][10]){
// Base case
if(i==0&&j==0){
dp[i][j] = cost[i][j];
return cost[i][j];
}
if(i<0||j<0){
return 0;
}
if(dp[i][j]!=0){
return dp[i][j];
}
if(i==0 && j>0){
dp[i][j] = cost[i][j] + MinCost(i,j-1,cost,dp);
return dp[i][j];
}
else if(i>0 && j==0){
dp[i][j] = cost[i][j] + MinCost(i-1,j,cost,dp);
return dp[i][j];
}
else{
int op1 = cost[i][j] + MinCost(i-1,j,cost,dp);
int op2 = cost[i][j] + MinCost(i,j-1,cost,dp);
dp[i][j] = min(op1,op2);
return dp[i][j];
}
}
int main(){
int cost[][4]={
{1,1,1,5},
{4,8,1,6},
{3,4,1,1},
{1,2,10,1}
};
int dp[10][10]={0};
int i,j;
cin>>i>>j;
cout<<MinCost(i,j,cost,dp)<<endl;
// for(int i=0;i<4;i++){
// for(int j=0;j<4;j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
return 0;
}