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

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<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的更多相关文章

  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. OpenJudge/Poj 1163 The Triangle

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

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

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

  5. POJ 1163 The Triangle 简单DP

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

  6. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

  7. poj 1163 The Triangle 搜索 难度:0

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

  8. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

  9. poj 1163 The Triangle 记忆化搜索

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

随机推荐

  1. .NET破解之谷歌地图下载助手-睿智版

    今天在整理文件是,发现手上还有个谷歌地图下载助手-睿智版,是C#写的.查了它们的官网,好像很久没有更新了,可能是垮了吧. 我把.NET程序破解分为三个阶段:软件分析,从软件使用上来分析功能的限制,即为 ...

  2. andriod 动态创建控件

    Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener( ...

  3. [ javascript html Dom image 对象事件加载方式 ] 对象事件加载方式

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  4. 读书笔记2013第10本:《学得少却考得好Learn More Study Less》

    <学得少却考得好Learn More Study Less>这本书最早是从褪墨网站上看到的,crowncheng翻译了全文.这本书介绍了不少学习方法,非常适合在校的学生,原文的作者Scot ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q51-Q53)

    Question 51You use a third-party site definition to create SharePoint sites.You need to add a Web Pa ...

  6. mac svn client 设置

    经过谷歌和百度N次后,终于搞定SVN的升级,Intellij Idea和Xcode5.1都可以正常使用. 步骤: 1. 下载Subverion的Max安装版.(推荐.使用其他brew和port都试过, ...

  7. 怎么向Xcode6 IOS8之后向项目中添加预编译文件

    苹果的XCode在6版本之后新建项目时取消了自动创建预编译头文件pch,该文件里存放的工程中一些不常被修改的代码,比如常用的框架头文件,这样做的目的提高编译器编译速度.我们可以往里面加入一些项目中都要 ...

  8. iOS之UI--主流框架的搭建--仿制QQ的UI框架

    使用XCode搭建多个控制器界面,一般在实际开发中建议超过四个控制器界面使用纯代码. 下面的实例其实已经超过了四个,总结详细步骤的目的,主要是更熟悉XCode的StoryBoard使用细节. 先直接上 ...

  9. Reverse反转算法+斐波那契数列递归+Reverse反转单链表算法--C++实现

    Reverse反转算法 #include <iostream> using namespace std; //交换的函数 void replaced(int &a,int & ...

  10. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...