Hdu 6212 Zuma【思维+区间Dp】
来源:互联网 发布:讨鬼传极优化补丁 编辑:程序博客网 时间:2024/06/14 03:30
Zuma
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 363 Accepted Submission(s): 103
Problem Description
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.
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 of0 and 1 describing the row of balls at the start.
Each test case contains a line with a non-empty string of
Output
For each test case, output the case number and the minimal balls required to insert in a line.
Sample Input
410101101001001100100100101001101011001100
Sample Output
Case #1: 4Case #2: 3Case #3: 3Case #4: 2
题目大意:
一共有两种颜色的祖玛游戏,每三个连在一起(或者大于三个)的球球就会被消除掉,问将这个字符串消除干净的最小吐球个数。
思路(思路源自:http://blog.csdn.net/zchahaha/article/details/78026747):
2006年百度之星复赛,zuma。原题。
①我们观察到数据范围不大,又要搞定最优解问题,我们不妨考虑区间Dp,设定Dp【i】【j】表示消除区间从i到j之间的球球的最小吐球个数。
同时将字符串处理到a【】,a【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】【j-1】+(a【i】+a【j】是否小于3));
3.中间有1个球球,然后消除掉其左右两侧的部分之后,3部分相撞消除:Dp【i】【j】=min(Dp【i】【j】,Dp【i+1】【k-1】+Dp【k+1】【j-1】);此时要求a【i】+a【j】<4,因为a【i】和a【j】都至少是1,如果相加大于等于4的话,无论先删除左侧区间还是右侧区间,都会构成2消的情况,而达不成三消的情况。
过程维护一下最优解即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char s[250];int a[250];int dp[250][250];int main(){ int t; int kase=0; scanf("%d",&t); while(t--) { scanf("%s",s); int n=strlen(s); int m=1; a[1]=1; for(int i=1;i<n;i++) { if(s[i]==s[i-1])a[m]++; else a[++m]=1; } for(int len=0;len<=m;len++) { for(int i=1;i<=m;i++) { int j=i+len; if(j<=m&&j>=1) { dp[i][j]=2*n; if(len==0) { dp[i][j]=3-a[i]; } else { for(int k=i;k<j;k++) { dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]); } if((j-i-1)%2==1) { if(a[i]+a[j]==2) dp[i][j]=min(dp[i][j],dp[i+1][j-1]+1); else dp[i][j]=min(dp[i][j],dp[i+1][j-1]); if(a[i]+a[j]<4) for(int k=i+2;k<j;k+=2) { if(a[k]==1) dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k+1][j-1]); } } } } } } printf("Case #%d: ",++kase); printf("%d\n",dp[1][m]); }}
阅读全文
0 1
- Hdu 6212 Zuma【思维+区间Dp】
- Hdu-6212 Zuma(区间DP)
- Hdu 6212 Zuma 区间DP
- 【2017青岛网络赛】hdu 6212 Zuma 1007 区间dp
- Codeforces_607B:Zuma(区间DP)
- HDU 6212 Zuma ACM/ICPC 2017 Qingdao Online(区间dp)
- 2017 ACM/ICPC Asia Regional Qingdao Online 1007 hdu 6212 Zuma 区间dp
- 2017 ACM/ICPC Asia Regional Qingdao Online 1007 hdu 6212 Zuma (区间dp)
- SPOJ-ZUMA(区间DP)
- CF607B: Zuma(区间dp)
- HDU6212 Zuma(区间dp)
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6212 1007 Zuma (区间DP)
- SPOJ 6340 ZUMA(区间DP)
- Codeforces 607B - Zuma (区间DP)
- Codeforces 607B Zuma 【区间dp】
- codeforces 607B Zuma 区间dp
- codeforces #336 D. Zuma (区间dp)
- CodeForces 607 B. Zuma(区间DP)
- 深入理解——继承
- web.xml is missing and <failOnMissingWebXml> is set to true
- 云数据库管理与数据迁移
- 机顶盒中动画、倒影、悬浮效果实现!!!!
- 循环赛制日程表
- Hdu 6212 Zuma【思维+区间Dp】
- Maven的依赖机制
- poj 2312 bfs+优先队列 入门题
- mybatis批量插入函数报“Parameter '__frch_item_0' not found. Available parameters are [list]”错误
- NPM使用介绍
- 使用Map向SQL中传递多个参数
- 图像二值化、提取边缘
- 菜鸟学设计模式----过滤器模式
- Codeforces Round #434 Did you mean...