uva 10118 记忆化搜索

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):
Step1 Initial Piles Step2 Take one from pile #2
Piles Basket Pocket Piles Basket Pocket
1 2 3 4 1 3 4
1 5 6 7 1 5 6 7
2 3 3 3 nothing nothing 2 3 3 3 2 nothing
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
Step3 Take one from pile #2 Step4 Take one from pile #3
Piles Basket Pocket Piles Basket Pocket
1 3 4 1 4
1 6 7 1 6 7
2 3 3 3 2 5 nothing 2 3 3 3 2 3 5 nothing
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
Step5 Take one from pile #2 Step6 Put two candies into his pocket
Piles Basket Pocket Piles Basket Pocket
1 4 1 4
1 6 7 1 6 7
2 3 3 2 3 3 5 nothing 2 3 3 2 5 a pair of 3
4 9 8 6 4 9 8 6
8 7 2 1 8 7 2 1
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 not 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

#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<set>
#include<time.h>
#include<vector>
#include<queue>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
typedef pair<int , int> pii;
#define maxn 42

int d[maxn][maxn][maxn][maxn];
int a[maxn][4];
int top[4];
int n;

void init()
{
    memset(d, -1, sizeof d);
    for(int i = 0; i < 4; i++)
    top[i] = 0;
}

int dfs(int cnt, bool *hash)
{
    int &res = d[top[0]][top[1]][top[2]][top[3]];
    if(res != -1) return res;
    if(cnt == 5) return res = 0;
    int ans = 0;
    for(int i = 0; i < 4; i++)
    {
        if(top[i] >= n) continue;
        top[i]++;
        int color = a[top[i]][i];
        if(hash[color])
        {
            hash[color] = false;
            int t = dfs(cnt - 1, hash);
            ans = max(ans, t + 1);
            hash[color] = true;
        }
        else
        {
            hash[color] = true;
            int t = dfs(cnt + 1, hash);
            ans = max(ans, t);
            hash[color] = false;
        }
        top[i]--;
    }
    return res = ans;
}


int main()
{
    while(~scanf("%d", &n) && n)
    {
        init();
        for(int i = 1; i <= n; i++)
        for(int j = 0; j < 4; j++)
        scanf("%d", &a[i][j]);

        bool hash[22];
        memset(hash, false, sizeof hash);
        printf("%d\n", dfs(0, hash));
    }
    return 0;
}

/*

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

*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值