POJ 1390(区间dp)

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

题意:给你一串方块,每个方块都有不同颜色,现在让你选择消方块的方式,使总得分最大。计分规则是如果k个相同颜色的方块连在一起,那么总得分就加上k * k。

题解:这是一道区间dp的练习题。
dp[i][j][max_len]代表着从i到j,且后面与j同色的最多方块数为max_len时,所得的最高得分。有两种转移状态。
1、不消j,让j处方块等待前面和j同色的方块一起消,dp[i][j][max_len] = dp[i][k][max_len + l[j]] + dp[k + 1, j - 1, 0],注意此时k+1到j-1之间的方块必须要消干净,且k处方块与j处方块同色。
2、消j,则dp[i][j][max_len] = dp[i][j - 1][0] + (max_len + l[j]) * (max_len + l[j]),注意如果要消j,则i到j - 1之间的方块也要都消掉才能得到最高分。
dp[i][j][max_len]的最终值为两种转移状态中分数较高的那个。
由于如果用递推的话,顺序各种混乱,则用记忆性递归。

思考:
1、一定要把原问题看仔细,分析子问题的时候分解全。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <functional>

using namespace std;

struct segment{
    int color, l;
};

const int maxn = 205;
int click[maxn][maxn][maxn];
segment seg[205];

int click_box(int i, int j, int max_len)//i到j这个区间里,在j之后与j颜色相同的最长串的长度
{
    if (click[i][j][max_len] != -1) {
        return click[i][j][max_len];
    }
    int result = 0;
    result = (seg[j].l  + max_len) * (seg[j].l + max_len);
    if (i == j) {
        click[i][j][max_len] = result;//别忘了存到数组里
        //printf ("%d %d %d %d\n", i, j, max_len, result);
        return result;
    }
    result += click_box (i, j - 1, 0);//将此块直接消除,此时前面的全部要消除掉
    for (int k = i; k < j; k++) {
        if (seg[k].color != seg[j].color) {
            continue;
        }
        int r = click_box (i, k, max_len + seg[j].l) + click_box (k + 1, j - 1, 0);
        result = max (r, result);
    }
    click[i][j][max_len] = result;
    //printf ("%d %d %d %d\n", i, j, max_len, result);
    return result;
}

int main()
{
    int kase = 1, t;
    scanf ("%d", &t);
    while (t--) {
        int n, cnow = 0, nseg = -1;
        memset (seg, 0, sizeof(seg));
        memset (click, -1, sizeof(click));
        scanf ("%d", &n);
        for (int i = 0; i< n; i++) {
            int c;
            scanf ("%d", &c);
            if (c != cnow) {
                nseg++;
                seg[nseg]. color = c;
                seg[nseg].l = 1;
                cnow = c;
                continue;//别忘了跳出循环
            }
            seg[nseg].l++;
        }
        /*for (int i = 0; i <= nseg; i++) {
            printf ("%d %d\n", seg[i].color, seg[i].l);
        }*/
        printf ("Case %d: %d\n", kase++, click_box (0, nseg, 0));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值