POJ_3176_Cow_Bowling_(数字三角形)_(动态规划)
2016-04-23 18:02
176 查看
描述
http://poj.org/problem?id=3176
给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少.
Cow Bowling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16826 | Accepted: 11220 |
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:7Then 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.
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
Input
Line 1: A single integer, NLines 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 rulesSample 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:7The highest score is achievable by traversing the cows as shown above.
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
Source
USACO 2005 December Bronze分析
记忆化搜索:
#include<cstdio> #include<algorithm> using std :: max; const int maxn=355; int n; int a[maxn][maxn],f[maxn][maxn]; void solve() { for(int i=n-1;i>=1;i--) { for(int j=1;j<=i;j++) { f[i][j]=max(f[i+1][j],f[i+1][j+1])+a[i][j]; } } printf("%d\n",f[1][1]); } void init() { scanf("%d",&n); for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { scanf("%d",&a[i][j]); } } for(int j=1;j<=n;j++) f [j]=a [j]; } int main() { #ifndef ONLINE_JUDGE freopen("cow.in","r",stdin); freopen("cow.out","w",stdout); #endif init(); solve(); #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); #endif return 0; }View Code
相关文章推荐
- LeetCode 第 69 题 (Sqrt(x))
- 杭电5672
- Java中的多线程
- 专题二1003
- java毕向东听课笔记23(集合框架-List集合具体对象)
- LeetCode 第 69 题 (Sqrt(x))
- java类加载机制的代码实例
- C++中的explicit关键字(转)
- CodeForces - 417A(思维题)
- Nginx压力测试工具之WebBench
- [Toddler's Bottle]-collision
- update-alternatives命令
- follow 开源项目关于NoClassDefFoundError错误的解决方法
- 19.fastDFS集群理解+搭建笔记
- 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)
- CodeForces - 416A (判断大于小于等于 模拟题)
- opencv 图像的深度和通道
- 14_1到1000之间的完全数
- GIT 分支管理:创建与合并分支、解决合并冲突
- Codeforces 346C Number Transformation II 构造