Guest User

Untitled

a guest
Oct 20th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. //Given a cost matrix cost[][] and a position (m, n) in cost[][], write a
  2. //function that returns cost of minimum cost path to reach (m, n) from (0, 0).
  3.  
  4. #include<iostream>
  5. #include<limits.h>
  6. using namespace std;
  7.  
  8. int min(int a, int b, int c)
  9. {
  10. if(a>b)
  11. return ((b>c)?c:b);
  12. else
  13. return ((a>c)?c:a);
  14. }
  15.  
  16. int minCost(int arr[][3], int m, int n)
  17. {
  18. if(m<0 || n<0)
  19. return INT_MAX;
  20. if(m==0&&n==0)
  21. return arr[0][0];
  22. return arr[m][n] + min(minCost(arr, m-1, n-1), minCost(arr, m, n-1), minCost(arr, m-1, n));
  23. }
  24.  
  25. int main()
  26. {
  27. int arr[][3] = {{1, 2, 3},
  28. {4, 8, 2},
  29. {1, 5, 3}};
  30. int m, n;
  31. cout<<" Find target location :- ";
  32. cin>>m>>n;
  33.  
  34. int sol = minCost(arr, m, n);
  35.  
  36. cout<<" Result is :- "<<sol<<endl;
  37.  
  38. system("PAUSE");
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment