HDU6212 Zuma(区间dp)

Zuma

传送门1
传送门2
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(1T100) 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


题意

一共有两种颜色的祖玛游戏,每三个连在一起(或者大于三个)的球球就会被消除掉,问将这个字符串消除干净的最小吐球个数。

分析

定义 dp[ i ][ j ] 表示消除区间从i到j之间的球球的最小吐球个数,再用 cnt[ i ] 表示第i个连续相同颜色的球球的个数。
那么显然,玩过祖玛的童鞋们都知道有三种消除一个区间方式(当然你没玩过也肯定想的出来 ):实际上也可以认为是两种
1.将区间分成两部分,各消除自身的部分:

dp[ i ][ j ]=min(dp[ i ][ j ]dp[ i ][ k ]+dp[k+1][ j ])

2.将中间区间全部消除掉之后,两端的相同颜色相撞删除:
dp[ i ][ j ]=min(dp[ i ][ j ]dp[i+1][j1]+(cnt[ i ]+cnt[ j ]3))

3.中间有1个球球,然后消除掉其左右两侧的部分之后,3部分相撞消除:
dp[ i ][ j ]=min(dp[ i ][ j ]dp[i+1][k1]+dp[k+1][j1])
此时要求 cnt[ i ]+cnt[ j ]<=3 ,因为 cnt[ i ] cnt[ j ] 都至少是1,如果相加大于3的话,无论先删除左侧区间还是右侧区间,都会构成第2种情况。
参考

CODE
#include<cstdio>
#include<cstring>
#define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++)
#define N 205
char s[N];
int cnt[N],dp[N][N];
inline void Min(int &x,int y) {if(x>y)x=y;}
int main() {
    int t;
    int cas=0;
    scanf("%d",&t);
    while(t--) {
        scanf("%s",s);
        int n=strlen(s),m=1;
        cnt[1]=1;
        FOR(i,1,n-1) {
            if(s[i]==s[i-1])cnt[m]=2;
            else cnt[++m]=1;
        }
        FOR(len,0,m)FOR(i,1,m) {
            int j=i+len;
            if(j<=m&&j>=1) {
                if(len==0)dp[i][j]=3-cnt[i];
                else {
                    dp[i][j]=n<<1;
                    FOR(k,i,j-1)Min(dp[i][j],dp[i][k]+dp[k+1][j]);//左边消左边的右边消右边的
                    if(!(len&1)) {
                        if(cnt[i]+cnt[j]==2)Min(dp[i][j],dp[i+1][j-1]+1);//左右相撞
                        else Min(dp[i][j],dp[i+1][j-1]);
                        if(cnt[i]+cnt[j]<=3)//左中右相撞
                            for(int k=i+2; k<j; k+=2)
                                if(cnt[k]==1)
                                    Min(dp[i][j],dp[i+1][k-1]+dp[k+1][j-1]);
                    }
                }
            }
        }
        printf("Case #%d: %d\n",++cas,dp[1][m]);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值