POJ 3176-Cow Bowling(DP||记忆化搜索)

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;
}
时间: 2024-12-29 11:11:46

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

Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1,?a2,?-,?an of positive integers: Initially, x?=?1 and y?=?0. If, after any step, x

poj 3176 Cow Bowling(dp)

递推式dp[i][j]=max( dp[i-1][j] , dp[i-1][j-1] )+a[i][j]; ①第一次做数塔,不会输入.数塔的输入 1 for(int i=1;i <= n;i++) 2 { 3 for(int j=1;j <= i;j++) 4 { 5 scanf("%d",&a[i][j]); 6 } 7 } 1 #include <iostream> 2 #include <cstring> 3 #include <

POJ 4968 DP||记忆化搜索

给出N个人的平局分X 根据GPA规则计算可能的最高平均GPA和最低平均GPA 可以DP预处理出来所有结果  或者记忆化搜索 DP: #include "stdio.h" #include "string.h" int inf=100000000; double a[11][1100],b[11][1100]; double Max(double a,double b) { if (a<b) return b; else return a; } double M

POJ 3176 Cow Bowling 保龄球 数塔问题 DP

题目链接:POJ 3176 Cow Bowling Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14044   Accepted: 9310 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though,

poj 1651 dp 记忆化搜索

题意: 给出n个整数a1,a2,-,an,要求从中取出中间的n-2个数(两端的数不能取),取出每个数的代价为它两边的数和它的乘积,问取出这n-2个数的最小代价为多少? 限制: 3 <= n <= 100; 1 <= ai <= 100 思路: dp 记忆化搜索 对于每个过程其实就是,枚举最后取的数a[i],然后把区间[l,r]分割成[l,i]和[i,r]两部分. dp[l][r]=min(gao(l,i)+a[left]*a[i]*a[right]+gao(i,r))

poj1664 dp记忆化搜索

http://poj.org/problem?id=1664 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t <= 20).以下每行均包含二个整数M和N,以空格分开.1<=M,N<=10. Output 对输入的每组数据M和N,用一行输出相应的K. Sample Input 1 7 3 Sample Output 8 /

11782 - Optimal Cut(树形DP+记忆化搜索)

题目链接:11782 - Optimal Cut 题意:按前序遍历给定一棵满二叉树,现在有k次,可以选k个节点,获得他们的权值,有两个条件: 1.一个节点被选了,他的子节点就不能选了. 2.最终选完后,根到所有叶子的路径上,都要有一个被选的节点. 思路:树形dp,dp[u][k]代表在结点u,可以选k个节点,那么就分两种情况 选u节点,dp[u][k] = node[u]; 选子节点之和,那么就把k次分配给左右孩子,dp[u][k] = max(dp[u][k], dp[u][i], dp[u]

[hihocoder 1033]交错和 数位dp/记忆化搜索

#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0,?a1,?...,?an?-?1,定义交错和函数: f(x)?=?a0?-?a1?+?a2?-?...?+?(?-?1)n?-?1an?-?1 例如: f(3214567)?=?3?-?2?+?1?-?4?+?5?-?6?+?7?=?4 给定 输入 输入数据仅一行包含三个整数,l,?r,?k(0?≤?l?≤?r?≤?1018,?|k|

UVA - 10817 Headmaster&#39;s Headache (状压dp+记忆化搜索)

题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所能教的所有科目. 2.已聘老师必须选,候选老师可选可不选. 3.dfs(cur, subject1, subject2)---求出在当前已选cur个老师,有一个老师教的科目状态为 subject1,有两个及以上老师教的科目状态为 subject2的情况下,最少的雇佣费用. dp[cur][subje