码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10118 Free Candies

时间:2017-08-30 15:39:11      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:some   iostream   color   algorithm   output   pairs   following   hat   put   

题目链接:https://vjudge.net/problem/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.

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.

 

题解:

  这个题目考虑暴力dp,设dp[i][j][k][h],表示第一列选到i,第二列选到j,第三列选到k,第四列选到h向下走还可以选的最大收益,当然转移就是枚举下一位,向哪里走,选哪个方块就可以了。

  当然肯定有人想为啥不用记篮子中有的球的颜色,因为只要i,j,k,h确定了,那么只要状态合法,那么篮子中的物品就是确定的,也就是说篮子的东西其实是无后效性的。

 

代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#define MAXN 45
using namespace std;
int dp[MAXN][MAXN][MAXN][MAXN];
bool b[MAXN][MAXN][MAXN][MAXN];
int num[5][MAXN],n;

int dfs(int i,int j,int k,int h,int lan,int cnt){
    if(b[i][j][k][h]) return dp[i][j][k][h];
    if(i>n||j>n||k>n||h>n) return 0;
    if(cnt>=5) return -(1<<30);
    b[i][j][k][h]=1;
    int maxx=0;
    if(lan&(1<<num[i+1][1])) maxx=max(maxx,dfs(i+1,j,k,h,lan^(1<<num[i+1][1]),cnt-1)+1);
    else maxx=max(maxx,dfs(i+1,j,k,h,lan|(1<<num[i+1][1]),cnt+1));
    if(lan&(1<<num[j+1][2])) maxx=max(maxx,dfs(i,j+1,k,h,lan^(1<<num[j+1][2]),cnt-1)+1);
    else maxx=max(maxx,dfs(i,j+1,k,h,lan|(1<<num[j+1][2]),cnt+1));
    if(lan&(1<<num[k+1][3])) maxx=max(maxx,dfs(i,j,k+1,h,lan^(1<<num[k+1][3]),cnt-1)+1);
    else maxx=max(maxx,dfs(i,j,k+1,h,lan|(1<<num[k+1][3]),cnt+1));
    if(lan&(1<<num[h+1][4])) maxx=max(maxx,dfs(i,j,k,h+1,lan^(1<<num[h+1][4]),cnt-1)+1);
    else maxx=max(maxx,dfs(i,j,k,h+1,lan|(1<<num[h+1][4]),cnt+1));
    dp[i][j][k][h]=maxx;
    return maxx;
}

int main(){
    while(1){
        memset(dp,0,sizeof(dp));
        memset(b,0,sizeof(b));
        memset(num,0,sizeof(num));
        scanf("%d",&n);
        if(n==0) break;
        for(int i=1;i<=n;i++)
        for(int j=1;j<=4;j++) 
        scanf("%d",&num[i][j]);
        printf("%d\n",dfs(0,0,0,0,0,0));
    }
}

 

UVA - 10118 Free Candies

标签:some   iostream   color   algorithm   output   pairs   following   hat   put   

原文地址:http://www.cnblogs.com/renjianshige/p/7452952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!