Advertisement
Farjana_akter

Untitled

Jul 29th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n,house[100][100],table[100][100];
  5.  
  6. int color(int indx,int col)
  7. {
  8. int ans,ans1,ans2;
  9. if(indx>n)
  10. {
  11. return 0;
  12. }
  13.  
  14. if(table[indx][col]!=0)
  15. return table[indx][col];
  16.  
  17. if(col==1)
  18. {
  19. ans1=house[indx][col]+color(indx+1,2);
  20. ans2=house[indx][col]+color(indx+1,3);
  21. }
  22. else if(col==2)
  23. {
  24. ans1=house[indx][col]+color(indx+1,1);
  25. ans2=house[indx][col]+color(indx+1,3);
  26. }
  27. else if(col==3)
  28. {
  29. ans1=house[indx][col]+color(indx+1,1);
  30. ans2=house[indx][col]+color(indx+1,2);
  31. }
  32. ans=min(ans1,ans2);
  33. table[indx][col]=ans;
  34. return ans;
  35. }
  36.  
  37. int main()
  38. {
  39. // freopen("in.txt","r",stdin);
  40. // freopen("out.txt","w",stdout);
  41. int i,j,cas,t,a,b,c,res;
  42. cin>>t;
  43. for(cas=1;cas<=t;cas++)
  44. {
  45. cin>>n;
  46. for(i=1;i<=n;i++)
  47. cin>>house[i][1]>>house[i][2]>>house[i][3];
  48. memset(table,0,sizeof(table));
  49. a=color(1,1);
  50. b=color(1,2);
  51. c=color(1,3);
  52. /* for(i=1;i<=n;i++){
  53. for(j=1;j<=3;j++)
  54. cout<<dp[i][j]<<" ";
  55. cout<<endl;
  56. }
  57. */
  58. res=min(a,min(b,c));
  59.  
  60. cout<<"Case "<<cas<<": "<<res<<endl;
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement