poj 1163 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
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<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的更多相关文章
- 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层 ...
- 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 之解题报告
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42232 Accepted: 25527 Description 7 3 ...
- poj 1163 The Triangle 搜索 难度:0
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37931 Accepted: 22779 De ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- poj 1163 The Triangle 记忆化搜索
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44998 Accepted: 27175 De ...
随机推荐
- SAP数据更新的触发
SAP 应用系统架构 应用层运行着DIALOG进程,每个DIALOG进程绑定一个数据库进程,DIALOG进程与GUI进行通信,每次GUI向应用服务器发送请求时都会通过dispatche ...
- Struts2(十七)验证框架二
一.实现注册验证 package com.pb.entity; import java.util.Date; /** * 用户实体类 * */ public class User { /** * 住址 ...
- SqlSever大数据分页
在sql sever中大数据的分页一直是难以处理的一块,利用id自增列分页也存在不足之处.从一个相对全面的分页看,sql sever2005中新增的row_number()函数解决了这个问题.还是从一 ...
- LeetCode 1 Two Sum(二分法)
题目来源:https://leetcode.com/problems/two-sum/ Given an array of integers, find two numbers such that t ...
- animation of android (2)
android Interpolator 首先是android系统提供的变换方式: 这些方式将转载一篇文章: 转: http://www.cnblogs.com/mengdd/p/3346003.ht ...
- CMD命令名详细大全
在运行菜单里键入CMD,就可以调出CMD命令窗口,有关某个命令的详细信息,请键入 HELP 命令名 ASSOC 显示或修改文件扩展名关联. AT 计划在计算机上运行的命令和程序.ATTRIB 显示或更 ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
- docker-4 Dockerfile的使用
Dockerfile FROM 基础镜像 MAINTAINER 维护这信息 RUN 运行什么命令,在命令前面加上RUN ADD 往里面加点文件,copy文件,会自动 ...
- JDK中的并发bug?
最近研究Java并发,无意中在JDK8的System.console()方法的源码中翻到了下面的一段代码: private static volatile Console cons = null; / ...
- myeclipse关闭html,jsp等页面的可视化编辑器
myeclipse打开html,jsp等页面时,有的是默认用可视化编辑器打开的,这样打开会显得很慢,只要关闭可视化编辑器就会快很多了,方法如下: 1,选择菜单: windows -> prefe ...