Cow Bowling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14210   Accepted: 9432

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
数字三角形问题。。能够自底向上坐dp dp[i][j]=ma[i][j]+max(dp[i+1][j],dp[i+1][j+1])
巨水 。。想当初半年前自己懵懵懂懂的刷dp啥都不懂。。哎  真是个悲伤的故事。。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,dp[maxn][maxn],ma[maxn][maxn];
void solve()
{
for(int i=0;i<n;i++)
dp[n-1][i]=ma[n-1][i];
for(int i=n-2;i>=0;i--)
for(int j=0;j<=i;j++)
dp[i][j]=max(ma[i][j]+dp[i+1][j],ma[i][j]+dp[i+1][j+1]);
printf("%d\n",dp[0][0]);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
for(int j=0;j<=i;j++)
scanf("%d",&ma[i][j]);
memset(dp,0,sizeof(dp));
solve();
}
return 0;
}

也能够自顶向下记忆化搜索。。然后状态数组含义都差点儿相同 。。个人觉着搜索比較好写。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n,dp[maxn][maxn],ma[maxn][maxn];
int dfs(int x,int y)
{
if(x==n-1)return ma[x][y];
if(dp[x][y]>=0)return dp[x][y];
dp[x][y]=0;
dp[x][y]+=(ma[x][y]+max(dfs(x+1,y),dfs(x+1,y+1)));
return dp[x][y];
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
for(int j=0;j<=i;j++)
scanf("%d",&ma[i][j]);
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(0,0));
}
return 0;
}

POJ 3176-Cow Bowling(DP||记忆化搜索)的更多相关文章

  1. POJ 3176 Cow Bowling(dp)

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

  2. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  3. poj 1191 棋盘分割(dp + 记忆化搜索)

    题目:http://poj.org/problem?id=1191 黑书116页的例题 将方差公式化简之后就是 每一块和的平方 相加/n , 减去平均值的平方. 可以看出来 方差只与 每一块的和的平方 ...

  4. POJ 1088 DP=记忆化搜索

    话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...

  5. poj1664 dp记忆化搜索

    http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...

  6. 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索

    题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...

  7. 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索

    [题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...

  8. [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树

    树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...

  9. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

随机推荐

  1. 创建Material Design风格的Android应用--使用Drawable

    下面Drawables的功能帮助你在应用中实现Material Design: 图片资源着色 在android 5.0(api 21)和更高版本号,能够着色bitmap和.9 png 通过定义透明度遮 ...

  2. rest-work-eat-study-rest-work-eat or rest-rest-work-work-eat-eat..

    words are for your heart. tks for my dear family's ok. Listening more  means not more talkive. 版权声明: ...

  3. java这些东西发展(1)-------大约ORA00604和ORA12705

    ******************************有关myEclipse和oracle在连接发生的一个问题********************************* 用户界面显示的评 ...

  4. 采用CSS3 Media Query技术适应Android平板屏幕分辨率和屏幕像素密度

    采用HTML5在开发移动应用程序满足各种需求Android分辨率和屏幕的平板设备密度,这是非常麻烦的过程,最终的解决方案是使用css media query,匹配相同的时间分辨率和屏幕像素密度.上进行 ...

  5. 【Unity 3D】学习笔记三十五:游戏实例——摄像机切换镜头

    摄像机切换镜头 在游戏中常常会切换摄像机来观察某一个游戏对象,能够说.在3D游戏开发中,摄像头的切换是不可或缺的. 这次我们学习总结下摄像机怎么切换镜头. 代码: private var Camera ...

  6. net中使用ETW事件

    在.net中使用ETW事件的方法   直到.net4.5,才有了比较便利的操作ETW的方法. 本文介绍的方法主要来源于Microsoft.Diagnostics.Tracing.TraceEvent官 ...

  7. 怎么样eclipse发达国家多重聚合关系maven项目和使用git管理

    最近使用的项目的开发maven,多于maven有项目之间有一定的联系,因此,创建一个单独的,然后,maven聚合管理. 项目采用git要管理代码.由于上传的代码集时,.gitignore不要上传文件. ...

  8. OCP-1Z0-051-标题决心-文章2称号

    2. View the Exhibit to examine the description for the SALES table. Which views can have all DML ope ...

  9. Ajax基础知识(一)

    随便在百度谷歌上输入Ajax都会出现一大堆的搜索结果,这已经不再是什么新奇的技术了.但若从一开始就学习了ASP.Net,使用功能齐全的Visual Studio集成开发工具,或许未必能对访问一个动态网 ...

  10. 配置Apacheserver

    配置Apacheserver 一.目的 能够有一个測试的server,不是全部的特殊网络服务都能找到免费得! 二.为什么我们要用"Apache"? Apache是眼下使用最广的we ...