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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109498.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C++ 单例模式_c 单例模式

    C++ 单例模式_c 单例模式原创文章,转载请注明出处。本文主要从单例的用处以及问题所做介绍

    2025年6月12日
    5
  • 动态规划优缺点_动态规划是解决

    动态规划优缺点_动态规划是解决C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。商人阿龙来到 C 国旅游。当他得知“同一种商品在不同城市的价格可能会不同”这一信息之后,便决定在旅游的同时,利用商品在

    2022年8月9日
    9
  • 用matlab绘制线性分段函数图像[通俗易懂]

    用matlab绘制线性分段函数图像[通俗易懂]假设线性分段函数如下所示在matlab中建立m文件:输入以下代码:x=0:0.01:5;y=zeros(size(x));fori=1:length(x)ifx(i)<0.9y(i)=0;elseifx(i)>=0.9&&x(i)<4.34y(i)=29.0698.*x(i)-26.1628;elsey(i)=100;endend

    2022年5月20日
    60
  • springboot实战第五章-springboot基础

    springboot实战第五章-springboot基础

    2021年5月16日
    134
  • mysql语句拼接字符串_C语言字符串输入及输出的几种方式

    mysql语句拼接字符串_C语言字符串输入及输出的几种方式MySQL字符串拼接可以使多个字段的值组成一个集合,不仅可以拼接字符串与字符串、空格、特殊符号甚至可以拼接中文文本,方便我们在不同场景下应用。本教详细讲解`CONCAT()`和它的扩展形式`CONCAT_WS()`在字符串拼接的实战场景中的应用。如果你的应用场景需要周期性重复展示,推荐使用卡拉云将你的代码工具化,详情见本文文末。

    2022年9月29日
    3
  • 模型评估之混淆矩阵

    模型评估之混淆矩阵在前面的文章中我们讲到了回归模型和分类模型的评估指标,区分了准确率和精确率的区别,并且比较了精确率和召回率内在的联系。本篇文章我们再来学习另外一个评估方法,即混淆矩阵(confusion_matrix)。在讲矩阵之前,我们先复习下之前在讲分类评估指标中定义的一些符号含义,如下:TP(TruePositive):将正类预测为正类数,真实为0,预测也为0 FN(FalseNegative):将正类预测为负类数,真实为0,预测为1 FP(FalsePositive):将负类预测为正类数,真实为

    2022年5月14日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号