POJ3176——Cow Bowling(动态规划)

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=;i<=N;i++)
for (int j=;j<=i;j++)
scanf("%d",&way[i][j]);
}
void Solve()
{
memset(dp,,sizeof(dp));
for (int i=;i<=N;i++)
for (int j=;j<=i;j++)
dp[i][j]=max(dp[i-][j],dp[i-][j-])+way[i][j];
}
void Output()
{
int ret=dp[N][];
for (int i=;i<=N;i++)
if (ret<dp[N][i]) ret=dp[N][i];
cout<<ret<<endl;
}
int main()
{
Input();
Solve();
Output();
return ;
}

Code(滚动数组优化 Mem:224K Tim:47MS):

 /*************************************************************************
> 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],dp[MAXN];
int N;
void Input()
{
cin>>N;
memset(dp,,sizeof(dp));
for (int i=; i<=N; i++)
{
for (int j=; j<=i; j++)
scanf("%d",&way[j]);
for (int j=i; j>=; j--)
dp[j]=max(dp[j],dp[j-])+way[j];
}
}
void Output()
{
int ret=dp[];
for (int i=; i<=N; i++)
if (ret<dp[i]) ret=dp[i];
cout<<ret<<endl;
}
int main()
{
Input();
Output();
return ;
}
上一篇:Codeforces 559C Gerald and Giant Chess【组合数学】【DP】


下一篇:用JS制作简易的可切换的年历,类似于选项卡