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: 

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 ( <= N <= ) rows, determine the highest possible sum achievable.
Input Line : A single integer, N Lines ..N+: Line i+ contains i space-separated integers that represent row i of the triangle.
Output Line : The largest sum achievable using the traversal rules
Sample Input
Sample Output

刚差不多刚看完动态规划,把小白书的例题大半都看懂了,也查了不少博客,然而做起题目来,连状态都不太会定义。

于是乎又查了起来,嗯看到了别人对状态的定义:way[i][j]表示以第i行j列的位置作为终点的路线的最大权值。 (注意区分初始化时的意义)

好的开始自己写状态转移方程:dp[i][j] = a[i][j] + max( dp[i+1][j], dp[i+1][j+1])

后来发现别人的更加简单:num[i][j] += max(num[i+1][j], num[i+1][j+1]) 

由于递推是沿着三角形向上的,就直接在原来的值上更新,只是得注意区分初始化时的意义

附上AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int num[][];
int main()
{
int n;
cin>>n;
for (int i = ; i <=n; i++)
for (int j = ; j <=i; j++)
cin>>num[i][j];
for (int i = n; i >= ; i--)
for (int j = ; j <= i; j++)
num[i][j] += max(num[i+][j], num[i+][j+]);
cout<<num[][]<<endl; return ;
}

动态规划_Cow Bowling_POJ-3176的更多相关文章

  1. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  2. POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)

    描述 http://poj.org/problem?id=3176 给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少. Cow Bowling Time Limi ...

  3. POJ 3176 Cow Bowling(dp)

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

  4. DP动态规划练习

    先来看一下经典的背包问题吧 http://www.cnblogs.com/Kalix/p/7617856.html  01背包问题 https://www.cnblogs.com/Kalix/p/76 ...

  5. 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280

    POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...

  6. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  7. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  8. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  9. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

随机推荐

  1. IIS 站点和应用池命令启动和停止

    在CMD下执行如下命令: IIS站点: 停止站点: C:\Windows\System32\inetsrv\appcmd.exe stop site “XXXX” 启动站点: C:\Windows\S ...

  2. 求你了,再问你Java内存模型的时候别再给我讲堆栈方法区了…

    GitHub 4.1k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 4.1k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 4.1k Star 的 ...

  3. C盘内存杀手:IDEA缓存文件!

    软件虽然装在D盘,C盘仍然还有一个文件夹 里面有两个文件夹: config 目录是 IntelliJ IDEA 个性化化配置目录,或者说是整个 IDE 设置目录.也是我个人认为最重要的目录,没有之一, ...

  4. 【Netty4】深入学习Netty

    Netty is an asynchronous event-driven network application framework  for rapid development of mainta ...

  5. Java面试常问问题及答案(非常详细)

    一:java基础1.简述string对象,StringBuffer.StringBuilder区分string是final的,内部用一个final类型的char数组存储数据,它的拼接效率比较低,实际上 ...

  6. docker开启2376端口CA认证及IDEA中一键部署docker项目

    嘿,大家好,今天更新的内容是docker开启2376端口CA认证及IDEA中一键部署docker项目... 先看效果 我们可以通过idea一键部署docker项目,还以通过idea的控制台实时查看容器 ...

  7. 了解selenium--(虫师的博客)

    Selenium is a portable software-testing framework for web applications. Selenium is composed of seve ...

  8. java常用基础(一)

    Java常用基础(一) 原文写于2017-12-02 输入输出 //输入 Scanner in = new Scanner(new BufferedInputStream(System.in)); i ...

  9. Linux 中文件和文件夹获取 MySQL 权限(SELinux)

    今天在 Linux 系统上移动 MySQL 的数据库目录 配置如下: /etc/my.cnf [mysqld]datadir=/home/mysqlsocket=/var/lib/mysql/mysq ...

  10. Dungeon Master POJ - 2251(bfs)

    对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...