Free Candies UVA - 10118(记忆化搜索)

Description

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):
在这里插入图片描述
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?

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.

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

解析

1、首先我们定义状态方程,dp[w][x][y][z],表示第一堆糖果拿了w个,第二堆拿了x个,第三堆拿了y 个,第四堆拿了z个。
2、那么如果想拿尽量多对,必须出现成对的立马拿走,因为篮子最多装5个。
3、每一次dfs我们都枚举拿掉每一堆最上面的一个,dfs的边界就是当前状态已经求出最大值或者是篮子满了
4、数组开的尽量合适,dfs的时候尽量不要传参,因为内存卡的比较紧。

AC代码

#include <bits/stdc++.h>
using namespace std;

const int len = 4;
const int maxn = 40 + 5;
int value[maxn][5], n;
int dp[maxn][maxn][maxn][maxn];
int height[5];
set<int> basket;

int dfs()
{
    if(basket.size() == 5)
        return dp[height[1]][height[2]][height[3]][height[4]] = 0;
    if(dp[height[1]][height[2]][height[3]][height[4]] != -1)
        return dp[height[1]][height[2]][height[3]][height[4]];

    int res = 0;
    for(int i = 1; i <= len; i++)
    {
        if(height[i] >= n)
            continue;

        height[i]++;
        if(basket.count(value[height[i] - 1][i]))
        {
            basket.erase(value[height[i] - 1][i]);
            res = max(dfs() + 1, res);
            basket.insert(value[height[i] - 1][i]);
        }
        else
        {
            basket.insert(value[height[i] - 1][i]);
            res = max(dfs(), res);
            basket.erase(value[height[i] - 1][i]);
        }
        height[i]--;
    }

    return dp[height[1]][height[2]][height[3]][height[4]] = res;
}

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    while(scanf("%d", &n) != EOF && n)
    {
        for(int i = 0; i < n; i++)
            for(int j = 1; j <= len; j++)
                scanf("%d", &value[i][j]);
        memset(dp, -1, sizeof dp);

        basket.clear();
        memset(height, 0, sizeof height);
        int res = dfs();
        printf("%d\n", res);
    }
    return 0;
}

发布了76 篇原创文章 · 获赞 18 · 访问量 2742

猜你喜欢

转载自blog.csdn.net/qq_43446165/article/details/104017895