poj1163(DP)and3176(DP)多串的…

来源:互联网 发布:手机怎样看淘宝的积分 编辑:程序博客网 时间:2024/05/01 13:34
http://poj.org/problem?id=3176
Cow Bowling
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 10012Accepted: 6594

Description

The cows don't useactual bowling balls when they go bowling. They each take a number(in the range 0..99), though, and line up in a standardbowling-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 andmoving "down" to one of the two diagonally adjacent cows until the"bottom" row is reached. The cow's score is the sum of the numbersof the cows visited along the way. The cow with the highest scorewins that frame.

Given a triangle with N (1 <= N <=350) rows, determine the highest possible sum achievable.

Input

Line 1: A singleinteger, N

Lines 2..N+1: Line i+1 contains i space-separated integers thatrepresent row i of the triangle.

Output

Line 1: The largestsum achievable using the traversal rules

Sample Input

573 88 1 02 7 4 44 5 2 6 5

Sample Output

30

Hint

Explanation of thesample:

          7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
The highest score is achievable by traversing the cows as shownabove.

Source

USACO 2005 December Bronze
这题白皮书上第7章上有
#include<stdio.h>
#include<string.h>
int map[500][500],dp[500][500],n;
int max(int a,int b)
{
 return a>b?a:b;
}
int d(int i,int j)
{
 if(dp[i][j]>=0)returndp[i][j];
 returndp[i][j]=map[i][j]+(i==n-1?0:max(d(i+1,j),d(i+1,j+1)));
}
int main()
{
 while(scanf("%d",&n)!=EOF)
 {
  int i,j;
  for(i=0;i<n;i++)
   for(j=0;j<=i;j++)
    scanf("%d",&map[i][j]);
  memset(dp,-1,sizeof(dp));
  printf("%d\n",d(0,0));
 }
 return 0;
}
原创粉丝点击