Advertisement
shamiul93

LightOJ 1047 - Neighour House

Mar 6th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1.  
  2. /**
  3. @author - Rumman BUET CSE'15
  4. problem - 1047 Neighour House
  5.  
  6. concept - Easy DP. 1200 solve. Even the dumbest people can solve.
  7. */
  8.  
  9.  
  10. #include<bits/stdc++.h>
  11. #define ll long long
  12. #define fo freopen("out.txt","w",stdout)
  13. #define fi freopen("in.txt","r",stdin)
  14. #define DEBUG printf("hi\n");
  15. #define DEBUG2 printf("bi\n");
  16. #define pi acos(-1)
  17. #define m 100000007
  18. #define d 0.000000001
  19. #define Min(a,b,c) min( a , min(b,c) )
  20.  
  21. using namespace std ;
  22.  
  23. ll rgb[25][4] ;
  24. ll dp[25][25] ;
  25. ll n ;
  26.  
  27. ll func(ll i, ll j)
  28. {
  29.     if(i >= n )
  30.         return 0  ;
  31.  
  32.     if(dp[i][j]!=-1)
  33.         return dp[i][j] ;
  34.     if(j == 0)
  35.     {
  36.         dp[i][j] = min( rgb[i][j] + func(i+1, j+1),rgb[i][j] + func(i+1, j+2)) ;
  37.         return dp[i][j] ;
  38.     }
  39.     else if(j == 1)
  40.     {
  41.         dp[i][j] = min( rgb[i][j] + func(i+1, j-1),rgb[i][j] + func(i+1, j+1)) ;
  42.         return dp[i][j] ;
  43.     }
  44.     else if(j == 2)
  45.     {
  46.         dp[i][j] = min( rgb[i][j] + func(i+1, j-1),rgb[i][j] + func(i+1, j-2)) ;
  47.         return dp[i][j] ;
  48.     }
  49. }
  50.  
  51. int main()
  52. {
  53. //    fi ;
  54. //    fo ;
  55.     ll T, t = 0  ;
  56.     scanf("%lld",&T) ;
  57.  
  58.     while(T--)
  59.     {
  60.         memset(dp, -1, sizeof(dp) ) ;
  61.         memset(rgb,0,sizeof(rgb)) ;
  62.         t++ ;
  63.         scanf("%lld",&n);
  64.  
  65.         for(ll i = 0 ; i < n ; i++)
  66.         {
  67.             for(ll j = 0 ; j < 3 ; j++)
  68.             {
  69.                 scanf("%lld",&rgb[i][j]) ;
  70.             }
  71.         }
  72.  
  73.         ll ans  ;
  74.         ans = Min(func(0, 0), func(0, 1), func(0, 2)) ;
  75.  
  76.         printf("Case %lld: %lld\n",t , ans) ;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement