POJ-1390-BlockS

来源:互联网 发布:曲子龙 网络尖刀 编辑:程序博客网 时间:2024/06/03 18:30

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5459 Accepted: 2262

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的大块(该大块可能是在合并过程中形成的,不妨就称其为ex_len), 且j的颜色和ex_len相同,在此情况下将 i 到j以及ex_len都消除所能得到的最高分。于是整个问题就是求: click_box(0,n-1,0)
求click_box(i,j,ex_len)时,有两种处理方法,取最优者并且假设j和ex_len合并后的大块称作 Q
1) 将Q直接消除,这种做法能得到的最高分就是:
click_box(i,j-1,0) + (len[j]+ex_len)2
2) 期待Q以后能和左边的某个同色大块合并。需要枚举可能和Q合并的大块。假设让大块k和Q合并,则此时能得到的最大分数是:
click_box(i,k,len[j]+ex_len) + click_box(k+1,j-1,0)
递归的终止条件是什么?
i == j;

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAXN = 205;struct Block{    int len;    int color;}block[MAXN];int dp[MAXN][MAXN][MAXN];int dfs(int start, int end, int add){    if (dp[start][end][add]) {        return dp[start][end][add];    }    int ret = block[end].len + add;    ret *= ret;    if (end == start) {        return dp[start][end][add] = ret;    }    ret += dfs(start, end - 1, 0);    for (int i = end - 1; i >= start; i--) {        if (block[i].color == block[end].color) {            int retS = dfs(start, i, block[end].len + add) + dfs(i + 1, end - 1, 0);            if (retS > ret) {                ret = retS;                break;                //return dp[start][end][add] = retS;            }        }    }    return dp[start][end][add] = ret;}int main(){    int t, n;    scanf("%d", &t);    for (int i = 1; i <= t; i++){        memset(dp, 0, sizeof(dp));        scanf("%d", &n);        int color, end = 0;        scanf("%d", &color);        block[end].color = color;        block[end].len = 1;        n--;        while (n--) {            scanf("%d", &color);            if (color == block[end].color) {                block[end].len++;            }            else {                end++;                block[end].color = color;                block[end].len = 1;            }        }        int score = dfs(0, end, 0);        printf("Case %d: %d\n", i, score);    }    return 0;} 
0 0
原创粉丝点击