zoukankan      html  css  js  c++  java
  • UVA

    题目链接: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
    ",dfs(0,0,0,0,0,0));
        }
    }
  • 相关阅读:
    广播接收者的生命周期?
    如何让自己的广播只让指定的 app 接收?
    在 manifest 和代码中如何注册和使用 BroadcastReceiver?
    请描述一下 BroadcastReceiver?
    说说 Activity、Intent、Service 是什么关系
    什么是IntentService?有何优点?
    Activity 怎么和 Service 绑定,怎么在 Activity 中启动自己对应的 Service?
    Service 是否在 main thread 中执行, service 里面是否能执行耗时的操作?
    两个 Activity 之间跳转时必然会执行的是哪几个方法?
    如何保存 Activity 的状态?
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7452952.html
Copyright © 2011-2022 走看看