POJ1390Blocks(DP+好题+抽空再来理解理解)
2024-08-29 23:36:39
Blocks
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 4744 | Accepted: 1930 |
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.
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.
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.
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
题意:通过点击某一颜色消除相邻的所有的这种颜色,得分为len*len,求最大分;
http://wenku.baidu.com/view/d956d2f30b4c2e3f5627630b分析:当看代码的时候却觉着原来就是这么简单
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
struct box_segment
{
int color,len;
};
box_segment segment[];
int score[][][];
int click_score(int start,int End,int extra_len)
{
if(score[start][End][extra_len] > )
return score[start][End][extra_len];
int result;
result = segment[End].len + extra_len;
result = result * result;
if(start == End)
{
score[start][End][extra_len] = result;
return score[start][End][extra_len];
}
result += click_score(start, End - , );
for(int i = End - ; i >= start; i--)
{
if(segment[i].color != segment[End].color)
continue;
int temp = click_score(start, i, segment[End].len + extra_len) + click_score(i + , End - ,);
if(temp <= result)
continue;
result = temp;
break;
}
score[start][End][extra_len] = result;
return score[start][End][extra_len];
}
int main()
{
int t,n,End;
int num = ;
scanf("%d", &t);
while(t--)
{
End = ;
scanf("%d", &n);
scanf("%d", &segment[End].color);
segment[End].len = ;
for(int i = ; i < n; i ++)
{
int color;
scanf("%d", &color);
if(color == segment[End].color)
segment[End].len++;
else
{
segment[++End].color = color;
segment[End].len = ;
}
}
memset(score, , sizeof(score));
printf("Case %d: %d\n", ++num,click_score(, End, ));
}
return ;
}
最新文章
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
- Android基础总结(四)
- 最近开始研究PMD(一款采用BSD协议发布的Java程序代码检查工具)
- ASP.NET C# 文件下载
- flash memory
- ecshop二次开发 给商品添加自定义字段
- Redis 数据序列化方法 serialize, msgpack, json, hprose 比较
- vs2015官方下载链接
- mongo-spark-读取不同的库数据和写入不同的库中
- 洛谷P4117 [Ynoi2018]五彩斑斓的世界 [分块,并查集]
- Appium 连手机失败Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localhost undefined:4724
- 【第二十五章】 springboot + hystrixdashboard
- mybatis待研究
- C# 文件下载断点续传
- IO多路复用之epoll(二)
- centos 安装php7
- hdu 4198:Quick out of the Harbour解题报告
- Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)
- MySql中的锁(表锁,行锁)
- SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)