POJ - 1390 &&UVA - 10559 Blocks(动态规划)

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

题意:有·N个方块排成一排,它们共有四种颜色(不重要),但相同颜色的不一定排在一起。点击一个方块,它以及左右相邻的(中间没有其他颜色)的同色方块会消失。得到的分数=消除的方块个数K^2;求可以得到的最大分数。

题解:《算法入门经典2》上的例题。

 按照线性动态规划的常见思路,设dp[i][j]表示子序列i~j的最大得分,但是却无法用dp[i][k]和dp[k][j]来计算dp[i][j],因为可能i~k和k~j各剩下一些,拼起来以后消除。如XAXBXCXDXEX。。是最后消除X的。

所以这事我们要建立三维数组dp[i][j][k],表示“原序列中的方块i~j右边再拼接上K个颜色等于j方块所得到的新序列”的最大得分.为了方便计算,我们还要color[i],表示不相邻的方块颜色数,len[i]表示相邻的颜色相同的方块数量。

状态转移方程两种:

1.单独消除,dp[i][j][k]=dp[i][j-1][0]+(len[j]+k)^2;

2.和区间段(i,j)中的某一块进行消除(要颜色一样能消除),假设i < = p <  j满足条件,则dp[i][j][k]=dp[i][p][k+len[j]]+dp[p+1][j-1][0]。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int t,n;
int color[250];
int len[250];
int dp[250][250][250];
int dfs(int l,int r,int k)
{
    if(dp[l][r][k])return dp[l][r][k];
    if(l==r)return dp[l][r][k]=(len[l]+k)*(len[l]+k);  ///这里K很重要,因为递归K不一定为0
    dp[l][r][k]=dfs(l,r-1,0)+(len[r]+k)*(len[r]+k);    //单独消除
    for(int i=l; i<r; i++)
    {
        if(color[i]==color[r])                         //满足状态转移方程2时
        {
            dp[l][r][k]=max(dp[l][r][k],dfs(i+1,r-1,0)+dfs(l,i,len[r]+k));  //两种消除方式的最大值
        }
    }
    return dp[l][r][k];
}
int main()
{
    int f=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int cnt=0;
        int a;
        memset(color,0,sizeof(color));
        memset(len,0,sizeof(len));
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a);
            if(color[cnt]==a)  
                len[cnt]++;
            else
            {
                cnt++;
                color[cnt]=a;
                len[cnt]++;
            }
        }
        printf("Case %d: %d\n",f++,dfs(1,cnt,0));
    }
    return 0;
}

转载于:https://www.cnblogs.com/zitian246/p/9123571.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值