Uva--10118(动规,记忆化搜索)

2014-08-24 22:24:57

 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 #2
PilesBasketPocketPilesBasketPocket
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
nothingnothing
1   3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
2nothing
Step3Take one from pile #2Step4Take one from pile #3
PilesBasketPocketPilesBasketPocket
1   3 4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 5nothing
1     4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 3 5nothing
Step5Take one from pile #2Step6put two candies into his pocket
PilesBasketPocketPilesBasketPocket
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 3 3 5nothing
1     4
1   6 7
2   3 3
4 9 8 6
8 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

5
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
1
1 2 3 4
3
1 2 3 4
5 6 7 8
1 2 3 4
0

Sample Output

8
0
3

思路:这个题。。。直接记忆化搜索,dp[k1][k2][k3][k4],表示用了第i堆前ki个所能达到的最大值。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 typedef long long ll;
 6 
 7 int dp[45][45][45][45];
 8 int col[5][45];
 9 int n;
10 
11 int Solve(int h1,int h2,int h3,int h4,int *arr){
12     if(dp[h1][h2][h3][h4])
13         return dp[h1][h2][h3][h4];
14     int f0 = 0,f1 = 0,f2 = 0,f3 = 0,f4 = 0;
15     for(int i = 1; i <= 5; ++i){
16         if(!f0 && arr[i] == 0)    f0 = i;
17         if(!f1 && h1 <= n && arr[i] == col[1][h1]) f1 = i;
18         if(!f2 && h2 <= n && arr[i] == col[2][h2]) f2 = i;
19         if(!f3 && h3 <= n && arr[i] == col[3][h3]) f3 = i;
20         if(!f4 && h4 <= n && arr[i] == col[4][h4]) f4 = i;
21     }
22     if(f0 == 0)
23         return 0;
24     int tmax = 0;
25     if(h1 <= n){
26         if(f1){
27             arr[f1] = 0;
28             tmax = max(tmax,Solve(h1 + 1,h2,h3,h4,arr) + 1);
29             arr[f1] = col[1][h1];
30         }
31         else{
32             arr[f0] = col[1][h1];
33             tmax = max(tmax,Solve(h1 + 1,h2,h3,h4,arr));
34             arr[f0] = 0;
35         }
36     }
37     if(h2 <= n){
38         if(f2){
39             arr[f2] = 0;
40             tmax = max(tmax,Solve(h1,h2 + 1,h3,h4,arr) + 1);
41             arr[f2] = col[2][h2];
42         }
43         else{
44             arr[f0] = col[2][h2];
45             tmax = max(tmax,Solve(h1,h2 + 1,h3,h4,arr));
46             arr[f0] = 0;
47         }
48     }
49     if(h3 <= n){
50         if(f3){
51             arr[f3] = 0;
52             tmax = max(tmax,Solve(h1,h2,h3 + 1,h4,arr) + 1);
53             arr[f3] = col[3][h3];
54         }
55         else{
56             arr[f0] = col[3][h3];
57             tmax = max(tmax,Solve(h1,h2,h3 + 1,h4,arr));
58             arr[f0] = 0;
59         }
60     }
61     if(h4 <= n){
62         if(f4){
63             arr[f4] = 0;
64             tmax = max(tmax,Solve(h1,h2,h3,h4 + 1,arr) + 1);
65             arr[f4] = col[4][h4];
66         }
67         else{
68             arr[f0] = col[4][h4];
69              tmax = max(tmax,Solve(h1,h2,h3,h4 + 1,arr));
70             arr[f0] = 0;
71         }
72     }
73     //printf("dp[%d][%d][%d][%d] : %d\n",h1,h2,h3,h4,tmax);
74     return dp[h1][h2][h3][h4] = tmax;
75 }
76 
77 int main(){
78     int now[6];
79     while(scanf("%d",&n) == 1 && n){
80          for(int i = 1; i <= n; ++i){
81             for(int j = 1; j <= 4; ++j){
82                 scanf("%d",&col[j][i]);
83             }
84         }
85         memset(now,0,sizeof(now));
86         memset(dp,0,sizeof(dp));
87         printf("%d\n",Solve(1,1,1,1,now));
88     }
89     return 0;
90 }

 

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3933946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值