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

102 篇文章 0 订阅
9 篇文章 0 订阅


Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=13&problem=1059&mosmsg=Submission+received+with+ID+15086449



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


题意:

有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果,

如果篮子里有两个相同的糖果,那么就可以把这两个(一对)糖果放进自己的口袋里,问最多能拿走多少对糖果。糖果种类最多20种. 

(黑书 148 免费糖果)

思路:

1. 这一题有点逆向思维的味道,dp[a, b, c, d] 表示从每堆中分别拿 a, b, c, d 个时,最多能拿多少个糖果;

2. 注意一点:当拿到 a, b, c, d 时,不能再拿了,此时结果肯定就会固定。利用这一点性质,采用记忆化搜索能有效的减少重复子结构的计算;

3. 题目是只有 0 0 0 0 这一个出发点的,根据这个出发点进行深搜,最终得出结果。

4. 本题可谓是深搜 + 记忆化搜索的经典,状态不是那么明显,子结构也不是那么好抽象,因为转移的末状态并不是固定的,是在不断的搜索中求出来的;



AC  code:

#include <iostream>
#include<cstdio>
#include<cstring>
#define N 41
using  namespace  std;
int  n;
int  dp[N][N][N][N];
int  mat[N][4];
 
int  DP( int  *top, int  st, int  k)
{
 
     int  &m=dp[top[0]][top[1]][top[2]][top[3]];
 
     if (m!=-1)
     return  m;
     if (top[0]==n&&top[1]==n&&top[2]==n&&top[3]==n||k==5)
     return  m=0;
     for ( int  i=0;i<4;i++)
     if (top[i]<n)
     {
         int  bit=1<<mat[top[i]][i];
         top[i]++;
         if (bit&st)
         m=max(m,DP(top,st-bit,k-1)+1);
         else  if (k<5)
         m=max(m,DP(top,st+bit,k+1));
         top[i]--;
     }
     return  m;
}
int  main()
{
     while ( scanf ( "%d" ,&n),n)
     {
         memset (dp,-1, sizeof (dp));
         for ( int  i=0;i<n;i++)
         for ( int  j=0;j<4;j++)
         scanf ( "%d" ,&mat[i][j]);
         int  top[5]={0};
         printf ( "%d\n" ,DP(top,0,0));
     }
     return  0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是几个经典的贪心算法例子的 Python 代码: 1. 零钱兑换问题 ''' 题目描述:给定一个整数金额和一些硬币,求出能够组成该金额的最少硬币数。 例如,当金额为 11 时,硬币面值为 [1, 2, 5],应返回 3(5元硬币1枚,2元硬币2枚)。 思路:贪心策略是每次尽可能使用面值最大的硬币,直到金额为 0。 ''' def coin_change(coins, amount): coins.sort(reverse=True) count = 0 for coin in coins: if amount == 0: break if coin <= amount: count += amount // coin amount = amount % coin return count if amount == 0 else -1 2. 活动选择问题 ''' 题目描述:假设有 n 个活动,每个活动都有一个开始时间和结束时间。选择一些活动,使得它们不冲突,且能够参加的活动数最多。 例如,有以下 6 个活动: [ (1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9) ] 其中,选择 (0, 6), (6, 7), (7, 8), (8, 9) 四个活动,即可参加的活动数最多。 思路:贪心策略是每次选择结束时间最早的活动,并且该活动与前面选择的活动不冲突。 ''' def activity_selection(activities): activities.sort(key=lambda x: x[1]) selected = [] end_time = -1 for activity in activities: if activity[0] >= end_time: selected.append(activity) end_time = activity[1] return selected 3. 分糖果问题 ''' 题目描述:给定两个列表,分别表示 n 个孩子的糖果数和 m 个糖果的大小。要求将糖果分给孩子,使得每个孩子最多只能分到一个糖果,并且尽可能多的孩子能够分到糖果。 例如,有 3 个孩子,糖果大小为 [1, 2, 3],糖果数为 [1, 1],则最多只能分配 2 个孩子,因为第三个孩子没有糖果可分。 思路:贪心策略是每次尽可能给糖果大小最小且能够满足该孩子的糖果。 ''' def candy(children, candies): children.sort() candies.sort() i = j = 0 count = 0 while i < len(children) and j < len(candies): if candies[j] >= children[i]: count += 1 i += 1 j += 1 return count

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值