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. Educational Codeforces Round 21 Problem A - C

    Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...

  2. C# 将文件转换为 Stream

    public Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileNam ...

  3. 【第二十六章】 hystrix-dashboard + turbine

    一.使用turbine的意义 引入多个hystrix stream: 1.使用hystrix-dashboard的可以添加多个stream的功能 图中添加的两个stream会在真正monitor的时候 ...

  4. [Pytorch]深度模型的显存计算以及优化

    原文链接:https://oldpan.me/archives/how-to-calculate-gpu-memory 前言 亲,显存炸了,你的显卡快冒烟了! torch.FatalError: cu ...

  5. UVa 1606 两亲性分子

    https://vjudge.net/problem/UVA-1606 题意:平面上有n个点,每个点为白点或者黑点.现在需放置一条隔板,使得隔板一侧的白点数加上另一侧的黑点数总数最大.隔板上的点可以看 ...

  6. fusion--RNAseq

    融合基因(Fusion gene)是指两个基因的全部或一部分的序列相互融合为一个新的基因的过程.其有可能是染色体易位.中间缺失或染色体倒置所致的结果. 异常的融合基因可以引起恶性血液疾病以及肿瘤.例如 ...

  7. 非[无]root权限 服务器 下安装perl以及perl模块--转载

    转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...

  8. sudo: /etc/sudoers is world writable sudo: no valid sudoers sources found, q...

    今天操作/etc/sudoers 文件,因为该文件只读,所以sudo chmod 777 /etc/sudoers ,结果可以修改这个文件了,但是导致所有用户的sudo都不能用了 是因为在Linux中 ...

  9. 常用 对象检测 api

    isPrototypeOf()    判断某个 proptotype 对象和某个实例之间的关系 alert(Cat.prototype.isPrototypeOf(cat1)); //true ale ...

  10. Android手机摄像头编程入门

    本讲内容:Android手机摄像头编程入门智能手机中的摄像头和普通手机中的摄像头最大的区别在于,智能机上的摄像头可以由程序员写程序控制, 做一些有趣的应用譬如,画中画,做一些有用的应用譬如二维码识别, ...