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 ;
}

POJ3176——Cow Bowling(动态规划)的更多相关文章

  1. POJ3176 Cow Bowling 2017-06-29 14:33 23人阅读 评论(0) 收藏

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19173   Accepted: 12734 Des ...

  2. Poj3176 Cow Bowling (动态规划 数字三角形)

    Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...

  3. POJ - 3176 Cow Bowling 动态规划

    动态规划:多阶段决策问题,每步求解的问题是后面阶段问题求解的子问题,每步决策将依赖于以前步骤的决策结果.(可以用于组合优化问题) 优化原则:一个最优决策序列的任何子序列本身一定是相当于子序列初始和结束 ...

  4. poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)

    经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...

  5. POJ3176:Cow Bowling(数字三角形问题)

    地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...

  6. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  7. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  8. Cow Bowling

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15585 Accepted: 10363 Descrip ...

  9. POJ 3176:Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 Desc ...

随机推荐

  1. Java实现Tire

    Trie,又称单词查找树或键树,是一种树形结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:最大限度地减少无谓的字符串比较,查询效率比 ...

  2. Careercup - Microsoft面试题 - 6543214668414976

    2014-05-11 02:56 题目链接 原题: Write a function called FooBar that takes input integer n and prints all t ...

  3. android开发,设置listview的高度无效

    一般是在item的layout中设置高度 android:layout_height="100dp" 但是发现这样后无效,因此找到解决办法,如下: android:minHeigh ...

  4. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  5. KMP算法原理

    前几天在看数据结构与算法,里面提到过kmp算法,一个超级经典的字符串匹配算法.虽然网上有一大堆关于kmp算法的介绍文章,但是我看过之后还是“不明觉厉”.所以打算自己写写,大家一起学习吧. 一.关于KM ...

  6. 第六周博客技术发表 C语言代码

    #include <stdio.h>       /*使用printf要包含的头文件*/#include <conio.h>void main(void)        /*主 ...

  7. ACM--South Pacific 2012

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=5 ...

  8. 【BZOJ】【2668】【CQOI2012】交换棋子

    网络流/费用流 跪跪跪,居然还可以这样建图…… 题解:http://www.cnblogs.com/zig-zag/archive/2013/04/21/3033485.html 考虑每个点的交换限制 ...

  9. navigationController.navigationBar.titleTextAttributes

    navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor ...

  10. WebService流行框架之Axis和CXF

    转自:http://www.cnblogs.com/snake-hand/archive/2013/06/09/3129915.html 前言 上节课我们对WebService进行了简单的介绍,对于其 ...