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. Zigbee学习

    (一)Zigbee简介和开发环境快速建立(IAR) 1.我不是很清楚控制链条,对于Zigbee不是太清楚 答案:CC2530 芯片上集成了 8051 内核(增强型) 2.性能特点:低速率远距离,这造就 ...

  2. C#中的基本类型理解

    1.C#把所有基本类型都封装成自己的类型了,如下图,int被封装成了一个struct结构体.如果定义一个int对象,是可以调用int结构体里的函数的 2.和C\C++不同,C#的char就是单纯的代表 ...

  3. Python3基础 tuple 创建空元组或者只有一个元素的元组 并 用乘法成倍扩充

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. MacBook PRO蓝牙无法搜索设备

    背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...

  5. LuoguP3183 [HAOI2016]食物链 记忆化搜索

    题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形如a1 b1a2 b2a3 b3.... ...

  6. Tomcat Connector

    转自: http://blog.csdn.net/aesop_wubo/article/details/7617416 如下图所示,Tomcat服务器主要有两大核心模块组成:连接器和容器,本节只分析连 ...

  7. Wireshark无法解析OpenFlow配置协议 解决方法

    在使用wireshark对OpenFlow交换机与FlowVisor的通信过程进行抓包分析的时候,在其选项中有openflow_v1选项,但Wireshark竟无法解析OpenFlow协议. 在查阅相 ...

  8. HTTP 随笔

    浏览器发送HTTP请求主要分为三部分请求行,Response Headers(响应头信息)和Request Headers(请求头信息). 请求行有分为三部分:请求方法,请求路径和请求协议 请求方法有 ...

  9. WCF 统一处理异常利用行为服务扩展

    https://www.cnblogs.com/niaowo/p/4727378.html using System; using System.Collections.Generic; using ...

  10. PYTHON HTML.PARSER库学习小结--转载

    前段时间,一朋友让我做个小脚本,抓一下某C2C商城上竞争对手的销售/价格数据,好让他可以实时调整自己的营销策略.自己之前也有过写爬虫抓某宝数据的经历,实现的问题不大,于是就答应了.初步想法是利用pyh ...