poj 1163 The Triangle 搜索 难度:0
| 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
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 <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的更多相关文章
- 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层 ...
- POJ 3126 Prime Path 广度优先搜索 难度:0
http://poj.org/problem?id=3126 搜索的时候注意 1:首位不能有0 2:可以暂时有没有出现在目标数中的数字 #include <cstdio> #include ...
- poj 1163 The Triangle 记忆化搜索
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44998 Accepted: 27175 De ...
- 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
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43809 Accepted: 26430 De ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
随机推荐
- vim 开启我们的Python之旅
1.在我们的Mac系统下打开“终端”,输入python,然后回车即可看到我们电脑是否安装了python,以及它的版本,这里我的是2.7.5版本,如果未安装请百度之.
- Email移动的原理
1.从数据库中得到被移动邮件的uid: 2.选择移动邮件所属folder,即SelectFolder; 3.调用copymessage(path,vmime::net::messageset::byU ...
- python函数作用域LEGB
我们的在学习Python函数的时候,经常会遇到很多定义域的问题,全部变量,内部变量,内部嵌入的函数,等等,Python是如何查找的呢?以及Python又是按照什么顺序来查找的呢?这里做一个顺序的说明 ...
- luogu P4396 [AHOI2013]作业
目录 题目 思路 错误&&傻叉 代码 题目 luogu 思路 每次都是插入比之前所有数字大的数,所以之前的答案就不会改变 用fhq-treap求出原序列,然后用树状数组依次算出每个值得 ...
- C#学习笔记(十一):类和对象
面向对象 为什么要面向对象: 1.和函数一样,把算法封装起来,方便复用 2.更好理解自己和别人写的代码 封装:数据.结构.逻辑的封装,方便复用 多态:同一个对象,同一种指令,不同的行为(反应) 继承: ...
- DataTables warning: table id=data-table - Requested unknown parameter '3' for row 0.
本文为博主原创,未经允许,不得转载: 在使用jquery 的datatable时,报错在页面弹出弹出框,并提示以下内容: DataTables warning: table id=data-table ...
- HTTP协议中TCP的三次握手 and HTTPS
https://www.cnblogs.com/zxh930508/p/5432700.html https://www.cnblogs.com/digdeep/p/4832885.html
- linux——通信指令学习简单笔记
一: 指令名称:write 指令所在路径:/usr/bin/write 执行权限:All User 语法:write <用户名> 功能描述:向另外一个用户发信息,以Ctrl+D作 为结束 ...
- Java jdbc访问sqlserver,oracle数据库 DEMO
1.JDBC访问Oracle数据库 public class Jdbc_Oracle { // 静态代码块,只会执行一次,类似C#静态构造方法 static { try { // 加载数据库驱动一次 ...
- c++ 匹配A容器中最先出现的b容器中的元素,返回iterator,(find_first_of)
#include <iostream> // std::cout #include <algorithm> // std::find_first_of #include < ...