POJ 1390 Blocks(DP)

本文介绍了一款名为'Blocks'的游戏,并提供了一种通过动态规划算法来计算游戏初始状态下所能获得的最高分数的方法。该算法考虑了点击方块时的不同策略,以实现分数最大化。

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

Blocks Time Limit:5000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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


问题分析:

求click_box(i,j,ex_len)时,有两种处理方法,取最优者
假设j和ex_len合并后的大块称作Q1)

将Q直接消除,这种做法能得到的最高分就是:click_box(i,j-1,0) + (len[j]+ex_len)*(len[j]+ex_len)

期待Q以后能和左边的某个同色大块合并。需要枚举可能和Q合并的大块。假设让大块k和Q合并,则此时能得到的最大
分数是:click_box(i,k,len[j]+ex_len) + click_box(k+1,j-1,0)

还有不清楚可以看下这个视频,特别详细blog.sina.com.cn/u/3266490431

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=210;
int dp[maxn][maxn][maxn];
int t,n,cas=0;
int color[maxn],len[maxn],block[maxn];
int solve(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[r]+k)*(len[r]+k);
    // 直接消除
    //cout << solve_dp(l, r-1, 0) + (k+len[r])*(k+len[r]) << endl;
    dp[l][r][k]=solve(l,r-1,0)+(len[r]+k)*(len[r]+k);
    // 先消除中间的, 合并 color[r], 在消除颜色为 r 的
    for(int i=l;i<r;i++){
        if(color[i]==color[r])
            dp[l][r][k]=max(dp[l][r][k],solve(l,i,k+len[r])+solve(i+1,r-1,0));
    }
    return dp[l][r][k];
}
int main()
{
    scanf("%d",&t);
    while(t--){
        int i,ans=0;
        memset(len,0,sizeof(len));
        memset(dp,0,sizeof(dp));
        for(scanf("%d",&n),i=0;i<n;i++) scanf("%d",&block[i]);
        color[0]=block[0],len[0]=1;
        for(int i=1;i<n;i++){
            if(block[i]==block[i-1]) len[ans]++;
            else{
                color[++ans]=block[i];
                len[ans]++;
            }
        }
        int sum=solve(0,ans,0);
        printf("Case %d: %d\n",++cas,sum);
    }
    return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值