poj3176——Cow Bowling(dp)
2016-04-21 18:33
344 查看
Description
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:
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
就是那个数塔
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
就是那个数塔
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> #include <bitset> #define MAXN 1005 using namespace std; int dp[400][400],a[400][400]; int main() { int n,i,j; while(~scanf("%d",&n)) { for(i=1;i<=n;++i) for(j=1;j<=i;++j) scanf("%d",&a[i][j]); for(i=1;i<=n;++i) dp [i]=a [i]; for(i=n-1;i>=1;--i) for(j=1;j<=i;++j) { dp[i][j]=max(dp[i+1][j]+a[i][j],dp[i+1][j+1]+a[i][j]); } printf("%d\n",dp[1][1]); } return 0; }
相关文章推荐
- Elasticsearch使用笔记
- md5和aesc加密方法
- 南京理工大学第八届程序设计大赛-sequence
- iOS9新特性
- 使用Eclipse的Generator MyBatis/iBatis Artifacts插件自动生成代码,与spring集成
- ArrayList list = new ArrayList(20);中的list扩充几次
- 学习周总结
- java学习之泛型
- 【CSS3进阶】酷炫的3D旋转透视
- 在Linux系统 CentOS 6 下安装KVM虚拟机
- struts2中的addActionError 、addFieldError、addActionMessage的方法
- 剑指offer之面试题12:打印1到最大的n位数
- solr入门之参考淘宝搜索提示功能优化拼音加汉字搜索功能
- java保留两位小数
- java并发之原子性、可见性、有序性
- 《Spark MLlib 机器学习》细节解释(持续更新)
- 利用JS实现点击按钮后图片自动切换
- 十二、单例模式Singleton(创建型模式)
- fzu2143
- APUE------进程控制