Free Candies - UVa 10118 dp

来源:互联网 发布:android和linux的关系 编辑:程序博客网 时间:2024/05/22 02:03

Problem B. Free Candies 

The Problem

Little Bob is playing a game. He wants to win some candies in it - as many as possible.

There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one pile into the basket, and if there're two candies of the same color in it ,he can take both of them outside the basket and put them into his own pocket. When the basket is full and there are no two candies of the same color, the game ends. If the game is played perfectly, the game will end with no candies left in the piles.

For example, Bob may play this game like this (N=5):

Step1Initial PilesStep2Take one from pile #2PilesBasketPocketPilesBasketPocket
1 2 3 41 5 6 72 3 3 34 9 8 68 7 2 1
nothingnothing
1   3 41 5 6 72 3 3 34 9 8 68 7 2 1
2nothingStep3Take one from pile #2Step4Take one from pile #3PilesBasketPocketPilesBasketPocket
1   3 41   6 72 3 3 34 9 8 68 7 2 1
2 5nothing
1     41   6 72 3 3 34 9 8 68 7 2 1
2 3 5nothingStep5Take one from pile #2Step6put two candies into his pocketPilesBasketPocketPilesBasketPocket
1     41   6 72   3 34 9 8 68 7 2 1
2 3 3 5nothing
1     41   6 72   3 34 9 8 68 7 2 1
2 5a pair of 3

Note that different numbers indicate different colors, there are 20 kinds of colors numbered 1..20.

'Seems so hard...'Bob got very much puzzled. How many pairs of candies could he take home at most?

The Input

The input will contain no more than 10 test cases. Each test case begins with a line containing a single integer n(1<=n<=40) representing the height of the piles. In the following n lines, each line contains four integers xi1,xi2,xi3,xi4 (in the range 1..20). Each integer indicates the color of the corresponding candy. The test case containing n=0 will terminate the input, you should not give an answer to this case.

The Output

Output the number of pairs of candies that the cleverest little child can take home. Print your answer in a single line for each test case.

Sample Input

51 2 3 41 5 6 72 3 3 34 9 8 68 7 2 111 2 3 431 2 3 45 6 7 81 2 3 40

Sample Output

803

题意:四列数字只能每列从上往下拿到篮子里,篮子里有相同的两个数字就归你所有,且篮子里最多有5个数字,问你最后能拿到多少对。

思路:f[a][b][c][d]表示在四列分别拿abcd个数的情况是否成立,如果成立,vector<int> dp[a][b][c][d]记录里面还有什么数字。另外我的代码比较麻烦,可以化成一段的,但是我懒得去做了…………反正四段也是一样的。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;vector<int> dp[45][45][45][45];int f[45][45][45][45],num[45][5];int main(){ int n,m,a,b,c,d,i,j,k,size1,size2,ans;  while(~scanf("%d",&n) && n)  { for(i=1;i<=n;i++)     for(j=1;j<=4;j++)      scanf("%d",&num[i][j]);    memset(f,0,sizeof(f));    ans=0;    f[0][0][0][0]=1;    for(a=0;a<=n;a++)     for(b=0;b<=n;b++)      for(c=0;c<=n;c++)       for(d=0;d<=n;d++)        if(f[a][b][c][d]==1)        { size1=dp[a][b][c][d].size();          ans=max(ans,a+b+c+d-size1);          if(a<n && f[a+1][b][c][d]==0)          { dp[a+1][b][c][d].clear();            for(i=0;i<size1;i++)             if(dp[a][b][c][d][i]!=num[a+1][1])              dp[a+1][b][c][d].push_back(dp[a][b][c][d][i]);            size2=dp[a+1][b][c][d].size();            if(size1==size2)            { if(size1<4)              { dp[a+1][b][c][d].push_back(num[a+1][1]);                f[a+1][b][c][d]=1;              }            }            else             f[a+1][b][c][d]=1;          }          if(b<n && f[a][b+1][c][d]==0)          { dp[a][b+1][c][d].clear();            for(i=0;i<size1;i++)             if(dp[a][b][c][d][i]!=num[b+1][2])              dp[a][b+1][c][d].push_back(dp[a][b][c][d][i]);            size2=dp[a][b+1][c][d].size();            if(size1==size2)            { if(size1<4)              { dp[a][b+1][c][d].push_back(num[b+1][2]);                f[a][b+1][c][d]=1;              }            }            else             f[a][b+1][c][d]=1;          }          if(c<n && f[a][b][c+1][d]==0)          { dp[a][b][c+1][d].clear();            for(i=0;i<size1;i++)             if(dp[a][b][c][d][i]!=num[c+1][3])              dp[a][b][c+1][d].push_back(dp[a][b][c][d][i]);            size2=dp[a][b][c+1][d].size();            if(size1==size2)            { if(size1<4)              { dp[a][b][c+1][d].push_back(num[c+1][3]);                f[a][b][c+1][d]=1;              }            }            else             f[a][b][c+1][d]=1;          }          if(d<n && f[a][b][c][d+1]==0)          { dp[a][b][c][d+1].clear();            for(i=0;i<size1;i++)             if(dp[a][b][c][d][i]!=num[d+1][4])              dp[a][b][c][d+1].push_back(dp[a][b][c][d][i]);            size2=dp[a][b][c][d+1].size();            if(size1==size2)            { if(size1<4)              { dp[a][b][c][d+1].push_back(num[d+1][4]);                f[a][b][c][d+1]=1;              }            }            else             f[a][b][c][d+1]=1;          }        }    printf("%d\n",ans/2);  }}



0 0
原创粉丝点击