美文网首页
四、动态规划(2)--方盒游戏

四、动态规划(2)--方盒游戏

作者: 安东可 | 来源:发表于2017-08-10 15:37 被阅读28次
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

struct block
{
    int color;
    int len;

};

block  segment[200];
int score[200][200][200]; //存放计算结果, 避免重复计算

int click_box(int start, int end, int ex_len){
    int i, result;
    if (score[start][end][ex_len] > 0)
        return score[start][end][ex_len];
    //最后一块喝ex_Len是同一样颜色的块,直接合并
    result = segment[end].len + ex_len;
    result = result * result; 
    //如果是最后一块,返回结果
    if (start == end){
            score[start][end][ex_len] = result;
            return score[start][end][ex_len];
    }

    //递归计算
    score[start][end][ex_len] = result+ click_box(start, end - 1, 0);
    //不直接消除最后的盒子
    for (i = start; i<end; i++){
        if (segment[i].color == segment[end].color)
            score[start][end][ex_len] = max(score[start][end][ex_len], click_box(start, i, segment[end].len + ex_len) + click_box(i + 1, end - 1, 0));
    }
    return score[start][end][ex_len];
}


int main(){
    int t, n, i, j, end, color;
    cout << "-------------------------" << endl;
    cout << "输入测试组(1- -15):";
    cin >> t;
    for (i = 0; i < t; i++){
        cout << "输入盒子数量:n(1--200):";
        cin >> n;
        end = 0;
        cout << "输入盒子颜色:";
        cin >> segment[end].color;
        segment[end].len = 1;
        for (j = 1; j < n; j++){
            cin >> color;
            if (color == segment[end].color) segment[end].len++;
            else
            {
                end++;
                segment[end].color = color;
                segment[end].len = 1;
            }
        }
        memset(score, 0, sizeof(score));
        cout << "-------------------------" << endl;
        cout << "case  " << i + 1 << " 的积分最高为: " << click_box(0, end, 0) << endl;
    }
    return 0;

}

【参考文档】

1.poj1390解题报告

相关文章

  • 四、动态规划(2)--方盒游戏

    【参考文档】 1.poj1390解题报告

  • 斐波那契数列

    递归解法 动态规划解法1 动态规划解法2

  • 4. 动态规划算法

    1. 动态规划算法总结2. 漫画:什么是动态规划?3.算法之动态规划4. 动态规划-算法

  • 动态规划(四)

    上一篇文章写了在动态规划中,最长子序列这一类型的题目。这一次我想要来讲一讲关于杨辉三角这一类型的题目,在LeetC...

  • 动态规划(四)

    0-1背包问题 有一个背包,他的容量为C(Capacity)。现在有n种不同的物品,编号为0...n-1,其中每一...

  • 动态规划(2)

    如果在只由'('和')'两种字符组成的字符串中,每一个符号都有合理的匹配,我们说这个字符串是完整的。问题1:怎么判...

  • 动态规划2

    参考:https://www.cnblogs.com/CheeseZH/p/8830482.html最长公共子序列...

  • Algorithm进阶计划 -- 动态规划(上)

    动态规划动态规划的基本原理动态规划的运用 1. 动态规划的基本原理 动态规划(Dynamic Programmi...

  • 跳跃游戏(贪心->动态规划)

    1.跳跃游戏(55-中) 题目描述:给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。数组中的每个...

  • 动态规划

    问题 什么样的问题可以用动态规划解决?解决动态规划问题的一般思考过程是什么样的?贪心、分治、回溯、动态规划这四种算...

网友评论

      本文标题:四、动态规划(2)--方盒游戏

      本文链接:https://www.haomeiwen.com/subject/agfhrxtx.html