Free Candies(记忆化+深搜)

Free Candies(记忆化+深搜)

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

题意: 一共有四堆糖果,给你一个数n,代表每堆有n个糖果。篮子最多能放5个糖果,每一次只能从每一堆的顶端取一个糖果放在篮子里,如果篮子里有两个颜色一样的糖果就可以把这两个糖果放进自己的口袋,如果篮子满了游戏就结束了,让你求出来你最多可以得到几对糖果

思路: 肯定要用记忆化搜索,直接搜索会超时

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n; 
int dp[50][50][50][50],a[50][50],book[50],top[50];
//dp 每堆拿第几个糖果时,最多能拿几对糖果 
//a  存放每一堆的糖果
//book 标记篮子中这个糖果是否出现
//top 记录每堆糖果最顶端的情况,即拿到了第几个 
int dfs(int x)//x为篮子中蜡烛的数量 
{
	int i,xx; 
	if(dp[top[0]][top[1]][top[2]][top[3]]!=-1)//记忆化 
	return  dp[top[0]][top[1]][top[2]][top[3]];
	if(x==5)
	return  dp[top[0]][top[1]][top[2]][top[3]];
	dp[top[0]][top[1]][top[2]][top[3]]=0;//没求之前是0对 
	int ans=0;
	for(i=0;i<4;i++)
	{
		if(top[i]==n)//这一堆糖果拿完了
		continue;
		xx=a[i][top[i]];//记录糖果的颜色
		top[i]++; //取下一个
		if(book[xx])//篮子中有这种颜色
		{
			book[xx]=0;//有相同的被取走
			ans=max(ans,dfs(x-1)+1);//篮子中糖果数量-1,被取走的对数+1 
		 	book[xx]=1;//还原 
		 } 
		else//篮子中没有这种颜色的糖果 
		{
			book[xx]=1;//现在有这种颜色的糖果了 
			ans=max(ans,dfs(x+1));//篮子中糖果数量+1 
			book[xx]=0;//还原 
		} 
		top[i]--;//没被取走 不太理解这里
	}
	return dp[top[0]][top[1]][top[2]][top[3]]=ans; 
}
int main()
{
	while(~scanf("%d",&n)&&n)
	{	
		memset(dp,-1,sizeof(dp));
		memset(book,0,sizeof(book));
		memset(top,0,sizeof(top));
		memset(a,0,sizeof(a));
	/*	for(int i=0;i<4;i++)
		for(int j=0;j<n;j++)
		scanf("%d",&a[i][j]);*///x1,x2,x3,x4而不是x1,x1,x1,x1 
		for(int i=0; i<n; i++)
            for(int j=0; j<4; j++)
                scanf("%d",&a[j][i]);
		printf("%d\n",dfs(0));
	
	}
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值