10118-free Candies (DP)

Source: Internet
Author: User

I think of it, once again ac,0.132s

Talk about my ideas, first of all to consider how to express the state, not difficult to find, the situation is very much, because how to take all the line, so had to increase the dimension, open four-dimensional array.

Because of the structure of the disorder, so the choice of memory search, by the former "headmaster's annoyance" inspired, even if we still maintain the candy basket, but we only need to open the four-dimensional array is enough to represent all the state, We can keep the situation in the basket and the number of candies in the basket in the parameters of the function to maintain it, then we are not difficult to use d[a][b][c][d] to indicate the maximum logarithm of candy in the pocket when each heap of candy is taken to the current state.

How to maintain the color of candy in the current basket? I think the method of other people's solution is many, I believe it is more simple or I this method, only need to use an integer through the binary operation to represent a set of methods, yes, because the current basket of candy color is different (the same will be in the pocket immediately), so it can be so simple to express.

Details to participate in the code:

#include <bits/stdc++.h>using namespace Std;int n,f[42][42][42][42],col[42][6];int dp (int a,int b,int c,int d,int  S,int cnt) {if (cnt = = 5) return 0;//boundary condition int& ans = f[a][b][c][d];//This is usually handy if the state is high (ans >= 0) return    Ans    The int s0;//Collection s maintains the candy in the current basket, because the candy color that exists in the basket is unique, so you can use the set to indicate ans = 0;        if (a <= n) {int v = col[a][1]; if (S & (1<<v)) {//The color already exists in the collection S0 = s ^ (1<<v);//removal of ans = max (ANS,DP (a+1,b,c,d,s0,cnt        -1) +1); } else {S0 = s |        (1<<v);//Join ANS = max (ANS,DP (a+1,b,c,d,s0,cnt+1));        }}//below if (b <= n) {int v = col[b][2];            if (S & (1<<v)) {S0 = s ^ (1<<v);        ans = max (ANS,DP (a,b+1,c,d,s0,cnt-1) +1); } else {S0 = s |            (1&LT;&LT;V);        ans = max (ANS,DP (a,b+1,c,d,s0,cnt+1));        }} if (c <= n) {int v = col[c][3];        if (S & (1<<v)) {    S0 = s ^ (1<<v);        ans = max (ANS,DP (a,b,c+1,d,s0,cnt-1) +1); } else {S0 = s |            (1&LT;&LT;V);        ans = max (ANS,DP (a,b,c+1,d,s0,cnt+1));        }} if (d <= n) {int v = col[d][4];            if (S & (1<<v)) {S0 = s ^ (1<<v);        ans = max (ANS,DP (a,b,c,d+1,s0,cnt-1) +1); } else {S0 = s |            (1&LT;&LT;V);        ans = max (ANS,DP (a,b,c,d+1,s0,cnt+1)); }} return ans;        int main () {while (~SCANF ("%d", &n) &&n) {memset (f,-1,sizeof (f));        for (int i=1;i<=n;i++) for (int j=1;j<=4;j++) scanf ("%d", &col[i][j]);    printf ("%d\n", DP (1,1,1,1,0,0)); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

10118-free Candies (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.