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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 浅谈时间轮算法[通俗易懂]

    浅谈时间轮算法[通俗易懂]时间轮在计算机世界中,只有待解决的问题变得大规模后,算法的价值才能够最大化的体现。时间轮算法可以将插入和删除操作的时间复杂度都降为O(1),在大规模问题下还能够达到非常好的运行效果。如果我们要实现一个定时任务该如何实现呢?最简单的方式就是使用一个任务队列来完成定时任务。具体实现细节下面详细展开。

    2022年9月27日
    2
  • jsp中JDBC连接MySQL数据库[通俗易懂]

    jsp中JDBC连接MySQL数据库[通俗易懂]前言:在进行网页制作时,难免会有数据库的使用,今天来讲一下jsp中利用JDBC连接MySQL数据库:::文章目录:一.JDBC:二.连接数据库:1.需要的包:2.加载驱动:3.连接数据库:一.JDBC:JDBC:Java数据库连接(JavaDatabaseConnectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是SunMicrosystems的商标。我们通常说的JDBC是面向关系型数据库的。(–

    2025年10月15日
    6
  • chrome添加扩展程序无效_vue兼容性问题

    chrome添加扩展程序无效_vue兼容性问题chrome扩展程序中以编程方式插入内容脚本不生效的问题

    2022年4月20日
    55
  • 干货|手把手教你写一个串口调试助手「建议收藏」

    干货|手把手教你写一个串口调试助手「建议收藏」摘要:前段时间发布了一个用QT写的串口调试助手,很多小伙伴在后台留言要源码。其实网上有很多免费开源的用QT的上位机,大家搜一下就能找到,为了大家方便学习QT以及如何写一个上位机,今天推荐一下学习资源,顺带带大家写一个非常简单的串口调试助手。相信很多小伙伴还没有接触过QT,如果想用QT写一个调试助手,首先是会一点C++语法。了解即可,也就是看得懂C++的代码。只要能看懂简单的C+++语法,就能很快的写一个串口调试助手。先推荐两个视频教程,感兴趣可以看看!1、B站Jomse工看完你基本知道串口调试助手

    2022年5月9日
    87
  • Cocos2d-x3.1 粒子效果演示样例[通俗易懂]

    Cocos2d-x3.1 粒子效果演示样例

    2022年1月18日
    48
  • Navicat for oracle创建数据库

    Navicat for oracle创建数据库前言其实在Oracle中的概念并不是创建数据库,而是创建一个表空间,然后再创建一个用户,设置该用户的默认表空间为我们新创建的表空间,这些操作之后,便和你之前用过的mysql数据库创建完数据库一模一样了(如果你用过mysql的话,当然如果Oracle是你用的第一个数据库系统,那上面这段话其实看不看并不重要)。但是,鉴于很多用过mysql的用户,在刚开始使用Oracle的时候都会不知道如何创建数据…

    2022年7月13日
    20

发表回复

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

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