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

22 篇文章 0 订阅

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
在这里插入图片描述
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
题意:有四堆糖果,每堆n个,有一个篮子,每次能从任意一堆顶部拿走一个糖果放入篮子里,如果拿的糖果篮子里已经有了,就可以把他们都装进口袋里,即答案++。问最多装走多少对糖果。

思路:第一次写四维的记忆化,纪念一下,hhh。其实我的思路就是…简单粗暴,其实我写的还是比较麻烦的。四维数组分别代表四堆糖果取走了多少个,dfs的时候还要保存的一个变量就是糖果数,因为篮子里最多放5个糖果,只有糖果数小于5才能继续往里面放。用vis数组记录现在篮子里已有的糖果状态,如果在篮子里有这个颜色的糖果,它的状态就是1。那么如果枚举时当前糖果状态为1,说明可以拿走它,把它的状态改变,然后糖果数-1。找出最大值就可以了。具体的看代码。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[42][42][42][42];
int color[45][10];
int vis[25];
int n;
int dfs(int a,int b,int c,int d,int sum)
{
	if(dp[a][b][c][d]!=-1)   //记忆化
		return dp[a][b][c][d];
	if(sum==5)	
		return 0; 
	int ans=0;
	int aa=color[a][1],bb=color[b][2],cc=color[c][3],dd=color[d][4];
	if(aa)   //aa=0的时候就是这一堆的糖果数已经>n了
	{
		if(vis[aa])
			vis[aa]=0,ans=max(ans,dfs(a+1,b,c,d,sum-1)+1),vis[aa]=1;   //记得递归完以后恢复状态
		else
			vis[aa]=1,ans=max(ans,dfs(a+1,b,c,d,sum+1)),vis[aa]=0;
	}
	if(bb)
	{
		if(vis[bb])
			vis[bb]=0,ans=max(ans,dfs(a,b+1,c,d,sum-1)+1),vis[bb]=1;
		else
			vis[bb]=1,ans=max(ans,dfs(a,b+1,c,d,sum+1)),vis[bb]=0;
	}
	if(cc)
	{
		if(vis[cc])
			vis[cc]=0,ans=max(ans,dfs(a,b,c+1,d,sum-1)+1),vis[cc]=1;
		else
			vis[cc]=1,ans=max(ans,dfs(a,b,c+1,d,sum+1)),vis[cc]=0;
	}
	if(dd)
	{
		if(vis[dd])
			vis[dd]=0,ans=max(ans,dfs(a,b,c,d+1,sum-1)+1),vis[dd]=1;
		else
			vis[dd]=1,ans=max(ans,dfs(a,b,c,d+1,sum+1)),vis[dd]=0;
	}
	return dp[a][b][c][d]=ans;
}
int main()
{
	while(~scanf("%d",&n) && n)
	{
		memset(color,0,sizeof color);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=4;j++)
				scanf("%d",&color[i][j]);
		memset(dp,-1,sizeof dp);   //记得初始化
		memset(vis,0,sizeof vis);
		printf("%d\n",dfs(1,1,1,1,0));
	}
	return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值