Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n,house[100][100],table[100][100];
- int color(int indx,int col)
- {
- int ans,ans1,ans2;
- if(indx>n)
- {
- return 0;
- }
- if(table[indx][col]!=0)
- return table[indx][col];
- if(col==1)
- {
- ans1=house[indx][col]+color(indx+1,2);
- ans2=house[indx][col]+color(indx+1,3);
- }
- else if(col==2)
- {
- ans1=house[indx][col]+color(indx+1,1);
- ans2=house[indx][col]+color(indx+1,3);
- }
- else if(col==3)
- {
- ans1=house[indx][col]+color(indx+1,1);
- ans2=house[indx][col]+color(indx+1,2);
- }
- ans=min(ans1,ans2);
- table[indx][col]=ans;
- return ans;
- }
- int main()
- {
- // freopen("in.txt","r",stdin);
- // freopen("out.txt","w",stdout);
- int i,j,cas,t,a,b,c,res;
- cin>>t;
- for(cas=1;cas<=t;cas++)
- {
- cin>>n;
- for(i=1;i<=n;i++)
- cin>>house[i][1]>>house[i][2]>>house[i][3];
- memset(table,0,sizeof(table));
- a=color(1,1);
- b=color(1,2);
- c=color(1,3);
- /* for(i=1;i<=n;i++){
- for(j=1;j<=3;j++)
- cout<<dp[i][j]<<" ";
- cout<<endl;
- }
- */
- res=min(a,min(b,c));
- cout<<"Case "<<cas<<": "<<res<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement