Blocks(poj 1390) 动态规划 方盒游戏 (升维——三维)

本文介绍了一款名为“Blocks”的游戏,并提供了解决方案来获取最高分。通过定义游戏规则和方盒片段的概念,文章详细解释了如何通过消除同色方盒片段获得积分,以及如何运用动态规划算法找到最佳消除策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Blocks  点击转到

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6197 Accepted: 2557

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 k*k points. for example, if you click on a silver box, the silver segment disappears, you got 4*4=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

 

1.题目含义:

     N个方盒(box)摆成一排,每个方盒有自己的颜色。连续摆放的同颜色方盒构成一个方盒片段(box segment)。下图中共有四个方盒片段,每个方盒片段分别有1、4、3、1个方盒玩家每次点击一个方盒,则该方盒所在方盒片段就会消失。若消失的方盒片段中共有k个方盒,则玩家获得k*k个积分。

2. 以前背包问题,对于一件物品来说,拿不拿的问题。今天的方盒游戏,则是消不消的问题。(不消除,因该考虑用另外一个变     量保存当前长度——升维)

     2.1消除:消除的话就是长度的平方,pow(lb[now].len,2);

     2.2 不消除:不消除的情况下,就是合并。但是合并,就要考虑和哪个方块进行合并?(因为要得到的分数尽可能地高,所以假设与k块进行合并,就for(int k=i;k<=j-1;k++)循环一遍,找最大值)

3.课件解析:

  

递归终止条件: (i==j) ,也就是从i到j属于一个方块。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstdlib>
using namespace std;
struct Block
{
	int color;
	int len;
};
Block b[210];
int s[210][210][210];
int getScore(int i,int j,int len)
{
	if(s[i][j][len]!=-1)
	   return s[i][j][len];
	int sum=pow((b[j].len+len),2);
	if(i==j)
	  return sum;
	sum=sum+getScore(i,j-1,0);//合并j块 
	for(int k=i;k<=j-1;k++)//合并j块后,形成新块与k块进行合并 
	{
		if(b[k].color!=b[j].color)
		   continue;
		int sumk=getScore(k+1,j-1,0);
		sumk=sumk+getScore(i,k,b[j].len+len);
		sum=max(sumk,sum);
	}
	s[i][j][len]=sum;
	return sum;
}
int main()
{
	int T;
	cin>>T;
	for(int t=1;t<=T;++t)
	{
		int n;
		memset(s,0xff,sizeof(s));
		cin>>n;
		int lastcolor=0;
		int r=-1;
		for(int i=0;i<n;i++)
		{
			int c;
			cin>>c;
			if(c!=lastcolor)
			{
				r++;
				b[r].len=1;
				b[r].color=c;
				lastcolor=c;
			}
			else
			  b[r].len++;
		}
		cout << "Case " << t << ": " << getScore(0,r,0) << endl;
	}
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值