
本文共 3287 字,大约阅读时间需要 10 分钟。
【POJ】1390 blocks 方块消除
原题
Time Limit: 5000MS
Memory Limit: 65536K
Description
Some of you may have played a game called ‘Blocks’. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The corresponding picture will be as shown below:
Figure 1
If some adjacent boxes are all of the same color, and both the box to its left(if it exists) and its right(if it exists) are of some other color, we call it a ‘box segment’. There are 4 box segments. That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in the segments respectively.
Every time, you can click a box, then the whole segment containing that box DISAPPEARS. If that segment is composed of k boxes, you will get kk points. for example, if you click on a silver box, the silver segment disappears, you got 44=16 points.
Now let’s look at the picture below:
Figure 2
The first one is OPTIMAL.
Find the highest score you can get, given an initial state of this game.
Input
The first line contains the number of tests t(1<=t<=15). Each case contains two lines. The first line contains an integer n(1<=n<=200), the number of boxes. The second line contains n integers, representing the colors of each box. The integers are in the range 1~n.
Output
For each test case, print the case number and the highest possible score.
Sample Input
2
9
1 2 2 2 2 3 3 3 1
1
1
Sample Output
Case 1: 29
Case 2: 1
翻译
时限: 5000MS
内存限制: 65536K
Description
你们中有些人可能玩过名为“积木”的游戏。连续有n个区块,每个方框都有一种颜色。这是一个示例:金,银,银,银,银,青铜,青铜,青铜,金。
相应的图片如下图:
图1
如果某些相邻的框都具有相同的颜色,并且其左侧(如果存在)和右侧(如果存在)的框都具有其他颜色,则我们将其称为“框段”。有4个框段。即:金,银,铜,金。段中分别有1、4、3、1个框。
每次您都可以单击一个框,然后单击包含该框的整个段。如果该段由k个框组成,则将获得k * k点。例如,如果您单击一个银色框,则银色部分消失,您得到4 * 4 = 16点。
现在让我们看下面的图片:
图2
第一个是最优的。
在此游戏的初始状态下,找到可获得的最高分数。
Input
第一行包含测试数量t(1 <= t <= 15)。每个案例包含两行。第一行包含整数n(1 <= n <= 200),即盒数。第二行包含n个整数,代表每个框的颜色。整数范围是1〜n。
Output
对于每个测试案例,请打印案例编号和可能的最高分数。
Sample Input
2
9
1 2 2 2 2 3 3 3 1
1
1
Sample Output
Case 1: 29
Case 2: 1
思路
设S(i,j,k)为(color[i],len[i]),(color[i+1],len[i+1]) … (color[j-1],len[j-1])的连续同色整段以及在一系列消除操作后j后接了k个颜色为color[j]的方块(color[j],len[j]+k)的一个颜色序列.
设f(i,j,k)表示消除S(i,j,k)这一个颜色序列可以得到的最大分值.
如果立刻将(color[j],len[j]+k)这一段消除,则转移为f(i,j-1,0)+(len[j]+k)2
如果len[j]+k暂时不消除而连接到上一个颜色相同的块上,则转移为f(i,p,k+len[j])+f(p+1,j-1,0).
其中color[p]=color[j], i<=p<j
f(i,j,k)=max(f(i,j-1,0)+(len[j]+k)2,f(i,p,k+len[j])+f(p+1,j-1,0))
color[p]=color[j], i<=p<j
代码
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int color[220],len[220],lenth,n,f[210][210][210];void input(){ int i,t; memset(f,-1,sizeof(f)); memset(color,0,sizeof(color)); memset(len,0,sizeof(len)); lenth=1; scanf("%d",&n); scanf("%d",&color[1]); len[1]=1; for(i=1;i<n;i++)//处理 { scanf("%d",&t); if (t==color[lenth]) { len[lenth]++; } else { color[++lenth]=t; len[lenth]=1; } } return;}int DP(int l,int r,int k){ int i; if (l==r) return (len[r]+k)*(len[r]+k); if (f[l][r][k]>=0) //记忆化 return f[l][r][k]; f[l][r][k]=DP(l,r-1,0)+(len[r]+k)*(len[r]+k); for (i=l;i<r;i++) if (color[i]==color[r]) f[l][r][k]=max(f[l][r][k],DP(l,i,len[r]+k)+DP(i+1,r-1,0));//状态转移方程 return f[l][r][k];}int main(){ int T,i; scanf("%d",&T); for(i=1;i<=T;i++) { input();//输入处理 printf("Case %d: %d\n",i,DP(1,lenth,0)); } return 0;}
发表评论
最新留言
关于作者
