Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Given a cost matrix cost[][] and a position (m, n) in cost[][], write a
- //function that returns cost of minimum cost path to reach (m, n) from (0, 0).
- #include<iostream>
- #include<limits.h>
- using namespace std;
- int min(int a, int b, int c)
- {
- if(a>b)
- return ((b>c)?c:b);
- else
- return ((a>c)?c:a);
- }
- int minCost(int arr[][3], int m, int n)
- {
- if(m<0 || n<0)
- return INT_MAX;
- if(m==0&&n==0)
- return arr[0][0];
- return arr[m][n] + min(minCost(arr, m-1, n-1), minCost(arr, m, n-1), minCost(arr, m-1, n));
- }
- int main()
- {
- int arr[][3] = {{1, 2, 3},
- {4, 8, 2},
- {1, 5, 3}};
- int m, n;
- cout<<" Find target location :- ";
- cin>>m>>n;
- int sol = minCost(arr, m, n);
- cout<<" Result is :- "<<sol<<endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment