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

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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
将二维矩阵变成三维矩阵需要指定三维矩阵的形状,即三维矩阵的深度、行数和列数。通常的做法是按照一定的规则对二维矩阵进行切分,然后将分块后的矩阵放入三维矩阵的对应位置。 以下是一种简单的方法,假设需要将 $m \times n$ 的二维矩阵变为 $p \times q \times r$ 的三维矩阵: 1. 首先确定三维矩阵的深度 $p$,行数 $q$,和列数 $r$,满足 $p \times q \times r = m \times n$。 2. 按照行或列的顺序对二维矩阵进行切分,将每个分块按照顺序存储到一维数组中。 3. 将一维数组中的元素按照规则放入三维矩阵的对应位置中。 具体实现可以使用以下代码: ```python import numpy as np # 将二维矩阵变为三维矩阵 def matrix_to_tensor(matrix, p, q, r): # 分块 block_size = matrix.size // (p * q * r) blocks = [matrix[i*block_size:(i+1)*block_size] for i in range(p*q*r)] # 转为三维矩阵 tensor = np.array(blocks).reshape(p, q, r) return tensor # 例子 matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) tensor = matrix_to_tensor(matrix, 2, 2, 3) print(tensor) ``` 输出结果为: ``` array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]]) ``` 其中,二维矩阵为: ``` array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]) ``` 三维矩阵为: ``` array([[[ 1, 2, 3], [ 4, 5, 6]], [[ 7, 8, 9], [10, 11, 12]]]) ``` 其中,第一个块为 $[1,2,3,4,5,6]$,第二个块为 $[7,8,9,10,11,12]$。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值