【POJ】1390 blocks 方块消除

【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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值