标签:des style blog io color ar os sp for
Cow Bowling
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:
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
题目大意:
输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线。
规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个。
解题思路:
动态规划。
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+data[i][j]
因为dp数组的值只与上一行有关,用滚动数组优化了一下。(直接开成一维的了。。。)
Code(Mem:1184K Tim:32MS):
/************************************************************************* > File Name: poj3176.cpp > Author: Enumz > Mail: 369372123@qq.com > Created Time: 2014年10月21日 星期二 19时38分18秒 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<list> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #define MAXN 351 using namespace std; int way[MAXN][MAXN],dp[MAXN][MAXN]; int N; void Input() { cin>>N; for (int i=1;i<=N;i++) for (int j=1;j<=i;j++) scanf("%d",&way[i][j]); } void Solve() { memset(dp,0,sizeof(dp)); for (int i=1;i<=N;i++) for (int j=1;j<=i;j++) dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+way[i][j]; } void Output() { int ret=dp[N][1]; for (int i=2;i<=N;i++) if (ret<dp[N][i]) ret=dp[N][i]; cout<<ret<<endl; } int main() { Input(); Solve(); Output(); return 0; }
Code(滚动数组优化 Mem:224K Tim:47MS):
1 /************************************************************************* 2 > File Name: poj3176.cpp 3 > Author: Enumz 4 > Mail: 369372123@qq.com 5 > Created Time: 2014年10月21日 星期二 19时38分18秒 6 ************************************************************************/ 7 #include<iostream> 8 #include<cstdio> 9 #include<cstdlib> 10 #include<string> 11 #include<cstring> 12 #include<list> 13 #include<queue> 14 #include<stack> 15 #include<map> 16 #include<set> 17 #include<algorithm> 18 #define MAXN 351 19 using namespace std; 20 int way[MAXN],dp[MAXN]; 21 int N; 22 void Input() 23 { 24 cin>>N; 25 memset(dp,0,sizeof(dp)); 26 for (int i=1; i<=N; i++) 27 { 28 for (int j=1; j<=i; j++) 29 scanf("%d",&way[j]); 30 for (int j=i; j>=1; j--) 31 dp[j]=max(dp[j],dp[j-1])+way[j]; 32 } 33 } 34 void Output() 35 { 36 int ret=dp[1]; 37 for (int i=2; i<=N; i++) 38 if (ret<dp[i]) ret=dp[i]; 39 cout<<ret<<endl; 40 } 41 int main() 42 { 43 Input(); 44 Output(); 45 return 0; 46 }
标签:des style blog io color ar os sp for
原文地址:http://www.cnblogs.com/Enumz/p/4094704.html