挑战2.3 Cow Bowling(POJ 3176)
来源:互联网 发布:unity3d要学多久 编辑:程序博客网 时间:2024/06/05 13:23
Cow Bowling
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14681 Accepted: 9763
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:
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5Then 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.
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
573 88 1 02 7 4 44 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 5The highest score is achievable by traversing the cows as shown above.
题目大意:
给你一个数字三角形,然后输出从a[1][1]到最后一行的某条线路上的最大的和,每次的行走路线只能走向下或者向右的路线。
解题思路:
裸裸的DP模板,数字三角形问题,我用的是从下往上的递推,也就是说,从倒数第n-1行开始,选择一个数字,使得这个数在加上第n行的数字后
他的和在目前这些数字中达到最大。然后每层都采用这样的思路进行循环,最后输出a[1][1]就表示了从第一行的第一个数字到最后一行的某个数字的和
的最大值.
状态:a[i][j]表示的是从第i行第j列的数字到最后一行的某个数字的和的最大值。
状态转移方程:a[i][j]+=max(a[i+1][j],a[i+1][j+1])
代码:
# include<cstdio># include<iostream># include<algorithm># include<cstring># include<string># include<cmath># include<queue># include<stack># include<set># include<map>using namespace std;typedef long long LL;# define inf 999999999# define MAX 350+4int a[MAX][MAX];LL ans;int n;int main(void){ while ( cin>>n ) { getchar(); for ( int i = 1;i <= n;i++ ) { for ( int j = 1;j <= i;j++ ) { cin>>a[i][j]; } } for ( int i = n-1;i >= 1;i-- ) { for ( int j = 1;j <= i;j++ ) { a[i][j]+=max( a[i+1][j],a[i+1][j+1]); } } cout<<a[1][1]<<endl; }return 0;}
0 0
- 挑战2.3 Cow Bowling(POJ 3176)
- POJ 3176-Cow Bowling [dp] 《挑战程序设计竞赛》2.3
- Cow Bowling(POJ 3176)
- POJ 3176 COW BOWLING
- POJ 3176 Cow Bowling
- poj 3176 Cow Bowling
- POJ 3176 Cow Bowling
- Poj 3176 Cow Bowling
- POJ 3176 Cow Bowling
- poj 3176 Cow Bowling
- poj 3176 Cow Bowling
- POJ 3176 Cow Bowling
- POJ 3176 Cow Bowling
- poj 3176 cow bowling
- POJ 3176 Cow Bowling
- POJ 3176 Cow Bowling
- POJ 3176 Cow Bowling
- poj 3176 Cow Bowling
- Multiply Strings
- 整理一些前端学习中好的书籍
- C# 集合类
- 关于解决这个问题:从一个点(x,y)绕另一个点(x0,y0)旋转任意角度A后得到的坐标(x1,y1)的坐标是多少?
- 第3周课后实践·阅读程序(3)-利用函数访问私有数据成员
- 挑战2.3 Cow Bowling(POJ 3176)
- c语言编写九九乘法表
- Spring使用JdbcTemplate、JdbcDaoSupport和返回表自增主键值
- 黑马程序员------OC中代码块(block)的总结
- 反转单链表的四种方法
- Linux开机过程
- 数据库习题二 作业完成者 王典2013211958
- Android之旅---广播(BroadCast)
- 概率统计(DP)