Poj 1163 The Triangle 之解题报告
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 42232 | Accepted: 25527 |
Description
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
对于这题若按照常规可能做不出来;但是用递推的思想就可以计算出来啦
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; const int maxn = +;
int vis[maxn][maxn];
int Triangle[maxn][maxn]; int max(int a,int b)
{
return a>b?a:b;
} int main(void)
{
int i,j,n;
while(~scanf("%d",&n))
{
for(i=;i<=n;++i)
for(j=;j<=i;++j)
scanf("%d",&Triangle[i][j]);
memset(vis,,sizeof(vis));
for(i=;i<=n;++i)
{
for(j=;j<=i;++j)
{
if(i==) vis[i][j]=Triangle[i][j];
else if(j==) vis[i][j] = vis[i-][j]+Triangle[i][j];
else if(j==i) vis[i][j] = vis[i-][j-]+Triangle[i][j];
else vis[i][j] = max(vis[i-][j]+Triangle[i][j],vis[i-][j-]+Triangle[i][j]);
}
}
int ans = ;
for(i=;i<n;++i)
ans = max(ans,vis[n][i]);
cout<<ans<<endl;
} return ;
}
Poj 1163 The Triangle 之解题报告的更多相关文章
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49955 Accepted: 30177 De ...
- POJ 1163 The Triangle(经典问题教你彻底理解动归思想)
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38195 Accepted: 22946 De ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
随机推荐
- 使用maven配置基本Mybatis
Mybatis 也称为是ibatis,主要体现在普通.底层SQL查询.存储过程.高级映射的持久化框架! 优点: 1.消除了几乎所 ...
- HTTP幂等性
http://www.cnblogs.com/weidagang2046/archive/2011/06/04/2063696.html 理解HTTP幂等性 基于HTTP协议的Web API是时下最为 ...
- CQRS学习——最小单元的Cqrs(CommandEvent)[其一]
[说明:博主采用边写边思考的方式完成这一系列的博客,所以代码以附件为准,文中代码仅为了说明.] 结构 在学习和实现CQRS的过程中,首要参考的项目是这个[http://www.cnblogs.com/ ...
- hibernate一对一双向外键关联
一对一双向外键关联:双方都持有对方的外键关联关系. 主控方和一对一单向外键关联的情况是一样的,主要的差异表现为,被空方需要添加: @OneToOne(mappedBy="card" ...
- [转载]C#中播放背景音乐几种的方法
最经在写winform程序,其中有用到播放背景音乐 特此收集了一些网上的教程: 1.调用非托管的dll using System.Runtime.InteropServices; //DllImpor ...
- javascript禁用与启用select标签(实用版)
<html><head><script type="text/javascript">function disable() { docume ...
- hdu 3952
因为一个小错 不过 好不爽........ #include <iostream> #include <fstream> using namespace std; struc ...
- Slider 滑动条效果
转载自:http://www.cnblogs.com/cloudgamer/archive/2008/12/24/Slider.html 这个滑动条(拖动条)效果,一开始是参考了BlueDestiny ...
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)0-------通过应用程序域AppDomain加载和卸载程序集
本博客中以“C#.Net 如何动态加载与卸载程序集(.dll或者.exe)”开头的都是引用莫问奴归处 微软装配车的大门似乎只为货物装载敞开大门,却将卸载工人拒之门外.车门的钥匙只有一把,若要获得还需要 ...