poj3176
一、题意:给定一些数,成三角形排列。从上往下走,每个数只能往它相邻的两个数走,一直走到底端得到一条线路。这条线路上的数的和最大是多少
二、思路:简单的动态规划。dp[i+1][j+1]:=以第i+1行j+1列为最后一个数字所能得到的最大和。所有的初始值均设为0,那么从上往下递推,可得到以每一个数为结尾的最大和,这样只要比较最后一行所能得到的最大和是多少即可。递推式为:dp[i+1][j+1]=max(dp[i][j],dp[i][j+1])+tri[i][j];
三、代码:
#include"iostream"
#include"stdio.h"
#include"string.h"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int tri[MAXN][MAXN];
int GetMaxSum(int n)
{
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])+tri[i][j];
}
}
int res=;
for(int j=;j<n;j++)
{
if(dp[n][j+]>res) res=dp[n][j+];
}
return res;
} int main()
{
int n;
while(scanf("%d",&n)==)
{
for(int i=;i<n;i++)
for(int j=;j<=i;j++)
scanf("%d",&tri[i][j]);
cout<<GetMaxSum(n)<<endl;
}
return ;
}
poj3176的更多相关文章
- POJ3176——Cow Bowling(动态规划)
Cow Bowling DescriptionThe cows don't use actual bowling balls when they go bowling. They each take ...
- 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280
POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...
- 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 ...
- Poj3176 Cow Bowling (动态规划 数字三角形)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- POJ3176 Cow Bowling 2017-06-29 14:33 23人阅读 评论(0) 收藏
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19173 Accepted: 12734 Des ...
- 【动态规划】POJ-3176
一.题目 Description The cows don't use actual bowling balls when they go bowling. They each take a numb ...
- POJ3176:Cow Bowling(数字三角形问题)
地址:http://poj.org/problem?id=3176 题目解析:没什么好说的,之前上课时老师讲过.从下往上找,每一个三角形的顶点可由两个角加上顶点的值 两种方式得到 ,用dp数组保存下最 ...
- POJ-3176
Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19864 Accepted: 13172 Des ...
- poj3176【简单DP】
其实就是简单递推对吧~ 贴一发记忆化搜索的- #include <iostream> #include <stdio.h> #include <string.h> ...
随机推荐
- Luogu 1379 八数码难题
吐槽:此题就是一点一点卡过去的 警告: 1.千万不能用dfs搜这种东西(dfs需要遍历所有状态才能找到最优解), 分分钟爆炸 2.写结构体的时候要综合判断&的加和不加 Code: // luo ...
- codefirst 关系处理
1.http://www.cnblogs.com/libingql/archive/2013/01/31/2888201.html 2.多对多 protected override void OnMo ...
- 【未整理】web.xml加载顺序.RP
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...
- LightOJ 1027 A Dangerous Maze (数学期望)
题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...
- struts2 、mybatis 、easyui 分页
rows page 控件自动提交这两个参数 pageSize number The page size. 10pageNumber number Show the page number when p ...
- .net IAsyncResult 异步操作
//定义一个委托 public delegate int DoSomething(int count); //BeginInvoke 的回调函数 private static void Execute ...
- HTML <area> 标签区域map标签
1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...
- 使用"*"通配符来选择文件
Include 方法和IncludeDirectory 方法中的搜索模式中指定的虚拟路径可以接受一个"*"通配符字符作为前缀或后缀,以在最后一个路径段.搜索字符串是大小写不敏感的. ...
- sql server重建系统数据库
方法一:https://bbs.csdn.net/topics/100013082 方法二:http://blog.51cto.com/jimshu/1095780 *** 方法三:https://b ...
- Android系列一: 环境搭建
相关软件 JAVA JDKAndroid StudioHAXM JDK的安装和Java环境变量的设置 1.JDK下载地址: http://www.oracle.com/technetwork/j ...