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

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

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

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. [2019杭电多校第七场][hdu6646]A + B = C(hash)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6646 题意为求a*10x+b*10y=c*10z满足公式的任意一组解x,y,z. 因为c有可能会由a+ ...

  2. Python 的 time 模块导入及其方法

    时间模块很重要,Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能,讲解一下Python 的 time 模块导入及其方法. 1,time 模块导入 import time; # ...

  3. Docker实战部署应用——Tomcat

    Tomcat 部署 拉取tomcat镜像 docker pull tomcat:8 创建tomcat容器 创建tomcat容器用于 Web应用,并且进行目录映射 docker run -id --na ...

  4. Python Paramiko模块使用

    1 执行远程命令 #!/usr/bin/python import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_polic ...

  5. ubuntu18.04 设置环境变量

    1.第一步:命令行输入 sudo gedit /etc/profile 2.第二步:将你想要设置环境变量的内容追加到文件结尾 例如:export JAVA_HOME=/usr/java/latest ...

  6. 网络拓扑_VLAN与Trunk配置

    实验目的: 1.实现VLAN10的两台主机互通; VLAN20的两台主机互通; 2.VLAN10与VLAN20主机不能互通. 拓扑图: 步骤: 1.依图配置PC1,PC2,PC3,PC4的IP,掩码, ...

  7. 网络拓扑_配置VLAN虚拟局域网

    目的: 1.创建VLAN10,VALN20,VLAN30; 2.将端口加入VLAN 3.查看VLAN信息 拓扑图: 步骤: 1.在三层交换机中创建VLAN10,VLAN20,VLAN30 <Hu ...

  8. Java8 时间处理类的使用实践(LocalDate...)

    有了它,谁还在用Date?Calendar? 其实也不能这么绝对,毕竟还没到那个程度上.Java8 新增了处理时间的一组类(LocalDate.LocalDateTime.LocalTime),刚开始 ...

  9. 使用Github 当作自己个人博客的图床

    使用Github 当作自己个人博客的图床 前提 本文前提: 我个人博客的草稿是存放在 github上的一个仓库 diarynote 截图存放的图片或者需要放在文章中图片,会固定存放在对应的文件夹中,我 ...

  10. nginx主配置文件学习,以及nginx的反向代理和负载均衡

    1.nginx.conf主配置文件学习 worker_processes : 表示nginx的进程数,根据CPU的核数来定义,起到优化的作用.通过cat /proc/cpuinfo来查看核数 even ...