UVA10118 Free Candies

题解:

记忆化搜索

如何判断一个数是否已经出现?应用位运算即可....

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define LL long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=200100;
const int mod=1e9+7;
const int INF=1e9;

int n;
int mat[4][45];
int dp[45][45][45][45];

int DP(int *top,int st,int k)
{
    int &m=dp[top[0]][top[1]][top[2]][top[3]];
    if(m>=0) 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[i][top[i]];
        top[i]++;
        if(bit&st) m=max(m,DP(top,st-bit,k-1)+1);
        else       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++) scanf("%d%d%d%d",&mat[0][i],&mat[1][i],&mat[2][i],&mat[3][i]);
        int top[5]={0};
        printf("%d\n",DP(top,0,0));
    }
    return 0;
}
时间: 2024-08-03 09:26:54

UVA10118 Free Candies的相关文章

UVA - 10118 Free Candies

题目链接: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 p

poj 3159 Candies

题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22516   Accepted: 6047 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought th

LeetCode解题思路:575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

hzau 1207 Candies

1207: Candies Time Limit: 2 Sec  Memory Limit: 1280 MBSubmit: 223  Solved: 31[Submit][Status][Web Board] Description Xiao Ming likes those N candies he collects very much. There are two kinds of candies, A and B. One day, Xiao Ming puts his candies i

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

poj 2886 Who Gets the Most Candies? Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from t

poj---(2886)Who Gets the Most Candies?(线段树+数论)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Accepted: 3224 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o

Zepto Code Rush 2014——Dungeons and Candies

题目链接 题意: k个点,每个点都是一个n * m的char型矩阵.对与每个点,权值为n * m或者找到一个之前的点,取两个矩阵对应位置不同的字符个数乘以w.找到一个序列,使得所有点的权值和最小 分析: 首先,这个图是一个无向图.求权值和最小,每个权值对应的是一条边,且每个点只能有一个权值即一条边,一个k个边,和生成树很像,但是需要证明不能有环形.不妨假设现在有三个点,每个点的最小边成环,这时候是不能找到一个序列使得每个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成