hdu 6212 Zuma 题解

神奇的传送门
题面:

Think about the Zuma Game. You have a row of at most 200 black(0) or white(1) balls on the table at the start. Each three consecutive balls never share the same colour. You also have infinite amount of black and white balls in your hand. On each turn, you can choose a ball in your hand and insert it into the row, including the leftmost place and the rightmost place. Then, if there is a group of three of more balls in the same colour touching, remove these balls. Keep doing this until no more balls can be removed.
Find the minimal balls you have to insert to remove all the balls on the table.


Input
The first line of input contains an integer T (1≤T≤100) which is the total number of test cases.
Each test case contains a line with a non-empty string of 0 and 1 describing the row of balls at the start.


Output
For each test case, output the case number and the minimal balls required to insert in a line.


Sample Input
4
10101
101001001
1001001001
01001101011001100


Sample Output
Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 2

题解:
考虑递归的过程,设为全部消掉的最少用,那么我们无疑有这几种消法:
1.中间有1个球球,然后消除掉其左右两侧的部分之后,3部分相撞消除:
Dp[i][j]=minDp[i][j]Dp[i][k]+Dp[k+1][j]
2.如果头与尾的颜色相同,那么:
Ⅰ)如果头尾加起来大于等于三个,就可以直接消,转移为 Dp[i][j]=minDp[i][j]Dp[i+1][j1]
Ⅱ)如果加起来不够三个,就可以补几个,转移为 Dp[i][j]=minDp[i][j]Dp[i+1][j1]+3a[i]a[j])
Ⅲ)无论头尾够不够,都可以在中间找一个,然后三消,因为我们定义有递归的感觉,所以其实三消与二消就包含了多消。转移为: Dp[i][j]=minDp[i][j]Dp[i+1][k1]+Dp[k+1][j1] 但是注意不能中间的那个分别与两头个数和都大于等于3个,因为这样不能三消,即可以推出要 a[i]+a[j]<4 ,就行了。
初始化把给 Dp[i][i]=3ball[i].sum
代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#define f(x,y,z) for (int (x);x<=y;x++)
using namespace std;
const int size = 1000; 
const int inf = 100000;
struct Ball{
    int sum,color;
}ball[size];int cnt;
int T , n ,kase=0;
int dp[size][size];
char s[size];
int main()
{
    scanf("%d",&T);
    while (    T--)
    {
        memset(s,NULL,sizeof s);
        memset(dp,0,sizeof dp);
        scanf("%s",s);    
        cnt=1;ball[1].color = (int)( s[0] -'0');ball[1].sum=1;
        for(int i=1;i<strlen(s);i++)
        if (ball[cnt].color != (int) (s[i]-'0') )  ball[++cnt]=(Ball){1,(int) (s[i]-'0')};
        else ball[cnt].sum++;

            for (int len = 0;len<=cnt;len++)
            {
                for (int i=1;i<=cnt;i++)
                {
                    int j = i + len;
                    if (j<=cnt&&1<=j)
                    {
                        dp[i][j] = inf;
                        if (i==j)
                        {
                            dp[i][j] = 3 - ball[i].sum;
                        }
                        else 
                        {
                            for (int k = i ; k <= j ; k++)
                            dp[i][j] = min(dp[i][j] , dp[i][k]+dp[k+1][j] );
                            if (ball[i].color == ball[j].color)
                            {
                                if (ball[i].sum+ball[j].sum>=3) dp[i][j] = min(dp[i][j] , dp[i+1][j-1]);
                                else dp[i][j] = min(dp[i][j] , dp[i+1][j-1] + 3- ball[i].sum-ball[j].sum); 
                                if (ball[i].sum+ball[j].sum < 4)
                                {
                                    for (int ii=i+1 ; ii<j ; ii++)
                                    if (ball[i].color==ball[ii].color && ball[ii].sum==1) 
                                    dp[i][j]=min(dp[i][j] , dp[i+1][ii-1] + dp[ii+1][j-1]);
                                }                
                            }    
                        }
                    }        
                }    
            }
     printf("Case #%d: ",++kase);
    printf("%d",dp[1][cnt]);
    putchar('\n');
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值