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

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 <cstring>
#include <algorithm>
using namespace std;
const int maxn=211;
int num[maxn][maxn];
int n,m;
int maz[maxn][maxn];
const int dx[2]={1,1},dy[2]={0,1};
int dfs(int sx,int sy){
if(num[sx][sy]!=-1)return num[sx][sy];
int tmp=maz[sx][sy];
for(int i=0;i<2;i++){
int tx=sx+dx[i],ty=sy+dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<tx+1){
int t=dfs(tx,ty);
tmp=max(t+maz[sx][sy],tmp);
}
}
return num[sx][sy]=tmp;
}
int main(){
int T=1;
while(T--&&scanf("%d",&n)==1){
for(int i=0;i<n;i++){
for(int j=0;j<i+1;j++){
scanf("%d",maz[i]+j);
}
}
memset(num,-1,sizeof(num));
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<i+1;j++){
if(num[i][j]==-1){
int tmp=dfs(i,j);
ans=max(ans,tmp);
}
}
}
printf("%d\n",ans);
}
return 0;
}

  

poj 1163 The Triangle 搜索 难度:0的更多相关文章

  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. POJ 3126 Prime Path 广度优先搜索 难度:0

    http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...

  4. poj 1163 The Triangle 记忆化搜索

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

  5. OpenJudge/Poj 1163 The Triangle

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

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

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

  7. POJ 1163 The Triangle 简单DP

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

  8. poj 1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43809   Accepted: 26430 De ...

  9. POJ 1321 棋盘问题 dfs 难度:0

    http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...

随机推荐

  1. Timer,TimerTask,Handler

    新建一个定时器线程,通过此线程每一秒发送数据到Handler,然后通过Handler来修改UI. 1.获得Handler,Timer,TimerTask对象. Handler handler=new ...

  2. C# MVC框架初学者

    推荐网站:http://blog.csdn.net/zhuyu19911016520/article/category/6318590

  3. Go第三篇之大话容器

    Go语言数组 数组(Array)是一段固定长度的连续内存区域 在 Go 语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化 Go 语言数组的声明 数组的写法如下: var 数组变 ...

  4. Python 中的几种矩阵乘法 np.dot, np.multiply, *【转】

    本文转载自:https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法1. 同线性代数中矩阵乘法的定义: np.d ...

  5. Git基础 —— Github 的使用

    Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Github 的利用 Gith ...

  6. ubuntu下安装mkfs.jffs工具

    一.环境 Os: ubuntu 16.04 二.安装 2.1安装依赖库 sudo apt install zlib1g-dev liblzo2-dev uuid-dev 2.2编译安装mtd-util ...

  7. Git 同时与多个远程库互相同步

    情形:有两个git服务器,比如github,gitosc,有个项目同时在两个服务器上,要互相同步 其实命令还是比较简单的,比如一个现有的git项目,在github,gitosc中分别创建好对应的项目. ...

  8. Python中通过csv的writerow输出的内容有多余的空行

    第一种方法 如下生成的csv文件会有多个空行 import csv #python2可以用file替代open with open("test.csv","w" ...

  9. gcc 编译出现 internal compiler error: Killed

    系统没有交换分区, 编译过程中内存耗尽, 导致了编译中断 …解决方式也很简单, 就是增加一个交换分区:       创建分区文件, 大小 2G dd if=/dev/zero of=/swapfile ...

  10. jq expando && $.data()

    1.使用隐藏控件或者是js全局变量来临时存储数据,全局变量容易导致命名污染,隐藏控件导致经常读写dom浪费性能 jQuery提供了自己的数据缓存方案,使用jQuery数据缓存方案,我们需要掌握$.da ...