poj 1163 The Triangle
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 43809 | Accepted: 26430 |
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>
#define ref(i,x,y) for(int i=x;i<=y;i++)
#define N 101
int a[N][N],f[N][N],n;
using namespace std;
int dfs(int i,int j){
if(f[i][j]>) return f[i][j];
return f[i][j]=a[i][j]+(i==n?:max(dfs(i+,j),dfs(i+,j+)));
}
int main(){
scanf("%d",&n);
ref(i,,n) ref(j,,i) scanf("%d",&a[i][j]);
dfs(,);
printf("%d\n",f[][]);
return ;
}
看不懂,没关系,还有一种
#include<cstdio>
#include<iostream>
#define ref(i,x,y) for(int i=x;i<=y;i++)
#define def(i,x,y) for(int i=x;i>=y;i--)
#define N 101
int a[N][N],f[N][N],n;
using namespace std;
int main(){
scanf("%d",&n);
ref(i,,n) ref(j,,i) scanf("%d",&a[i][j]);
ref(i,,n) f[n][i]=a[n][i];
def(i,n-,) ref(j,,i) f[i][j]+=a[i][j]+max(f[i+][j],f[i+][j+]);
printf("%d\n",f[][]);
return ;
}
poj 1163 The Triangle的更多相关文章
- 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层 ...
- 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 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- Poj 1163 The Triangle 之解题报告
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42232 Accepted: 25527 Description 7 3 ...
- poj 1163 The Triangle 搜索 难度:0
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37931 Accepted: 22779 De ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- poj 1163 The Triangle 记忆化搜索
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44998 Accepted: 27175 De ...
随机推荐
- Microsoft Dynamics CRM 2013 --选项集的多选
由于从Microsoft Dynamics CRM 2011到Microsoft Dynamics CRM 2013,界面的风格发生了很大的变化 故原先在2011上开发的选项集多选在2013上面已经不 ...
- C++map类型
map是键-值对的集合,可以理解为关联数组,可以使用键作为下标来获取一个值 本文地址:http://www.cnblogs.com/archimedes/p/cpp-map.html,转载请注明源地址 ...
- Spring(五)AOP简述
一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...
- Spring(一)简述
一.Spring简述 一段费话 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2 ...
- Xcode找Library位置
- 如何避免Activity 被杀死
我们都知道,在android系统中,内存不足的时候,系统是可以杀死任何暂停.停止或者销毁的Activity.这就意味着基本上没有在前台的Activity都会面临被关闭的可能. Android系统之所以 ...
- Python 常用函数time.strftime()简介
time.strftime()可以用来获得当前时间,可以将时间格式化为字符串等等 格式命令列在下面:(区分大小写) %a 星期几的简写%A 星期几的全称%b 月分的简写%B 月份的全称%c 标准的 ...
- Python yield 使用浅析(iterable generator )
http://blog.csdn.net/preterhuman_peak/article/details/40615201 如何生成斐波那契數列 斐波那契(Fibonacci)數列是一个非常简单的递 ...
- for循环练习题
■■■■■■■■■■■■■■■■■■■■■■■■■ 代码: <script> for(i=0;i<5;i++) { for(j=0;j<5;j++) { document.wr ...
- Java GC 面试问题
转自:http://icyfenix.iteye.com/blog/715301 这个帖子的背景是今晚看到je上这张贴:http://www.iteye.com/topic/715256,心血来潮写下 ...