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

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

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的更多相关文章

  1. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  3. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  4. POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49955   Accepted: 30177 De ...

  5. POJ 1163 The Triangle 简单DP

    看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...

  6. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

  7. poj 1163 The Triangle 搜索 难度:0

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37931   Accepted: 22779 De ...

  8. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

  9. poj 1163 The Triangle 记忆化搜索

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44998   Accepted: 27175 De ...

随机推荐

  1. java随机生成指定的位数

    /** * 获取count个随机数 * @param count 随机数个数 * @return */ public static String game(int count){ StringBuff ...

  2. 转:HTTP 1.1与HTTP 1.0的比较

    原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...

  3. ubuntu解决libstdc++.so.6: cannot open shared object file: No such file or directory:问题

    解决libstdc++.so.6: cannot open shared object file: No such file or directory:原因在于,在13.10 版本中,ia32_lib ...

  4. 【原/转】UITableview性能优化总结

    UITableView作为ios中使用最频繁的控件之一,其性能优化也是常常要面对的,尤其是当数据量偏大并且设备性能不足时.本文旨在总结tableview的几个性能优化tips,并且随着认识的深入,本文 ...

  5. xib命名注意事项--防止被其他控制器意外地 当做默认的 view了

    注意: 1.创建的xib如果不是想给指定的控制器做view的话,命名就要注意了! 2.最好是不要命名和控制器名字相关的xib. 如下举例说明一下: - (void)touchesBegan:(NSSe ...

  6. <极客学院>视频教程学习笔记-iOS中CALayer的使用

    <1>CALayer简介 1.CALayer一般作为UIView的容器而使用. 2.CALayer是一个管理者图片载体(image-based content)的层结构 3.直接修改单独创 ...

  7. widowns 列出文件目录树结构 tree命令

    TREE [drive:][path] [/F] [/A] /F  显示每个文件夹中文件的名称.(带扩展名) /A  使用 ASCII 字符,而不使用扩展字符. tree -f > list.t ...

  8. dig与dns基本理论——解析和缓存

    DNS(Domain Name System,域名系统)也许是我们在网络中最常用到的服务,它把容易记住的域名,如 www.google.com 翻译成人类不易记住的IP地址,如 173.194.127 ...

  9. LU分解,Javascript代码

    ///A 为矩阵,这里写成一维数组,如 [1],[1,2,3,4] function GetLU(a) { var n = a.length;//矩阵的总数据数目 var s = Math.sqrt( ...

  10. 颠覆式前端UI开发框架:React

    转自:http://www.infoq.com/cn/articles/subversion-front-end-ui-development-framework-react/ 基于HTML的前端界面 ...