动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果。(可以用于组合优化问题)

优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束状态的最优决策序列。

只有满足优化原则的问题才可以利用动态算法进行求解,因为只有全局最优解法等于其每个子问题的最优才可以分阶段进行求解。

The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:

          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30

Hint

Explanation of the sample:

          7

*

3 8

*

8 1 0

*

2 7 4 4

*

4 5 2 6 5

The highest score is achievable by traversing the cows as shown above.

这道题的思路解法是:记录n-1层的每个元素值到n层的最大距离,再计算n-2层到n层=n-2到n-1+n-1到n,然后n-3到n=n-3到n-2+n-2到n;一直如此循环直到第一层

 

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=355;
int main()
{
int sum=0,a[maxn][maxn],b[maxn][maxn],c[maxn][maxn]; //a记录每个位置牛的编号 ,b[i][j]记录从(i,j)位置往下走的最大编号和但不包括a[i][j]本身
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
scanf("%d",&a[i][j]); //存储每头牛的编号
for(int i=n-1;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
if(a[i+1][j]+b[i+1][j]>=a[i+1][j+1]+b[i+1][j+1]) b[i][j]=b[i+1][j]+a[i+1][j],c[i][j]=0; //c记录走的路径
else b[i][j]=b[i+1][j+1]+a[i+1][j+1],c[i][j]=1;
}
}
/*cout<<a[1][1]<<endl; //注释掉的内容所走的路径
for(int i=1,j=1;i<n;i++)
{
cout<<a[i+1][j+c[i][j]]<<endl;
j=j+c[i][j];

}*/
cout<<a[1][1]+b[1][1]<<endl;
return 0;
}

POJ - 3176 Cow Bowling 动态规划的更多相关文章

  1. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  2. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  3. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  4. poj 3176 Cow Bowling(dp基础)

    Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...

  5. poj 3176 Cow Bowling(区间dp)

    题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...

  6. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  7. POJ3176——Cow Bowling(动态规划)

    Cow Bowling DescriptionThe cows don't use actual bowling balls when they go bowling. They each take ...

  8. POJ 3176:Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 Desc ...

  9. POJ 3176 简单DP

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16448 Accepted: 10957 Descrip ...

随机推荐

  1. Kosaraju算法 有向图的强连通分量

    有向图的强连通分量即,在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  2. BZOJ 3252题解(贪心+dfs序+线段树)

    题面 传送门 分析 此题做法很多,树形DP,DFS序+线段树,树链剖分都可以做 这里给出DFS序+线段树的代码 我们用线段树维护到根节点路径上节点权值之和的最大值,以及取到最大值的节点编号x 每次从根 ...

  3. HihoCoder - 1093 小Hi和小Ho (SPFA)

    描述 万圣节的晚上,小Hi和小Ho在吃过晚饭之后,来到了一个巨大的鬼屋! 鬼屋中一共有N个地点,分别编号为1..N,这N个地点之间互相有一些道路连通,两个地点之间可能有多条道路连通,但是并不存在一条两 ...

  4. ORM数据库的增删改查

    数据库可视化工具: https://sqlitestudio.pl/index.rvt from app01 import models def orm(request): #增加数据 # 方法1: ...

  5. ASP.NET中Literal控件的使用方法(用于向网页中动态添加内容)

    原文:https://www.jb51.net/article/82855.htm 可以将 Literal 控件用作网页上其他内容的容器.Literal 控件最常用于向网页中动态添加内容.简单的讲,就 ...

  6. 03-CSS颜色、文本、字体、边框、背景

    # Css颜色,文本字体 ## css颜色表示法1.颜色名表示,比如:red 红色,gold 金色 2.16进制数值表示,比如:#ff0000 表示红色,这种可以简写成 #f00 3.RGB颜色: 红 ...

  7. Ionic创建混合App(一)

    前言 最近公司要开始做App项目,最终选定了ionic开发方案,在这里我将学习的过程记录在这里,一方面避免自己忘记,另一方面方便大家交流学习.这里我们采用的是 Ionic2 + Angular2 : ...

  8. vue访问外部接口设置代理,解决跨域(vue-cli3.0)

    vue-cli3.0搭建的项目,平时访问内部接口配置了拦截器,今天需要调用天气预报的外部接口,发现跨域问题,通过配置代理解决. 1.在vue.config.js中配置代理 module.exports ...

  9. vue v-model 的使用

    Vue的核心特性之一是双向绑定,vue的响应式原理是实现了数据->视图 v-bind只能实现数据的单向绑定 ,从M自动绑定到V 使用v-model 可以实现表单元素的双向绑定,且只能应用到表单元 ...

  10. java指定若干个网络图片,打包为zip下载

    应项目要求需要将多个存在某url地址的图片,打包为zip下载下来 public void download(HttpServletRequest request, HttpServletRespons ...