一、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.



二、问题分析

        DP问题,和之前做的1088滑雪属于同一类问题。解这个问题与解其它的DP问题几乎没有什么两样。第一步找到问题的“状态”,第二步找到“状态转移方程”,然后基本上问题就解决了。首先,我们要找到这个问题中的“状态”是什么?我们必须注意到的一点是,到达终点坐标的方式最多有两种:根据数组存放的特点有向下和斜右下两个方向。所以,终点之前的点,可以由终点向上和斜左上两个方向。从两个方向中选出路径值最大的。而所选出的这个坐标又可以当成是一个子问题递归求解。由于使用了记忆化存储,所以可以先直接查表,如果表中存在子问题的解则直接返回,否则就按上面的分析过程找到最大路径并存储。经过上面的分析,很容易可以得出问题的状态和状态转移方程。

S[i][j]=arr[x][y] + max(S[i-1][j], if i>1 ; S[i-1][j-1], if j>1,x>1 ),x和y为当前结点的值。

小弟Poj第二十题,水分不少,一水到底。

三、java代码

import java.util.Scanner;

public class Main {
private int[][] a;
private int[][] m;
private int n; private void init(){
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
a=new int[n+1][n+1];
m=new int[n+1][n+1]; for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
a[i][j]=cin.nextInt();
m[i][j]=-1;
}
}
} public int searchMaxRoute(int x,int y){
if( m[x][y]!=-1)
return m[x][y];
else{
int max=0;
if(x>1){
max=Math.max(max,searchMaxRoute(x-1,y));
}
if(x>1 && y>1){
max=Math.max(max,searchMaxRoute(x-1,y-1));
}
m[x][y]=max+a[x][y];
return m[x][y];
}
}
public int getMaxHeight(){
int temp;
int Max=-1;
for(int i=n;i>=1;i--){
for(int j=1;j<=n;j++){
temp=searchMaxRoute(i,j);
if(Max< temp)
Max=temp;
}
}
return Max;
}
public static void main(String[] args) {
Main m=new Main();
m.init();
System.out.println(m.getMaxHeight());
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj1163 The Triangle(动态规划求最大权值的路径)的更多相关文章

  1. hdu 4293 dp求最大权值不重合区间

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> # ...

  2. 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)

    这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...

  3. 奔小康赚大钱 HDU - 2255(最大权值匹配 KM板题)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...

  6. POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)

    题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of ...

  7. POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解

    题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...

  8. hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...

  9. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

随机推荐

  1. thinkphp5, 模板继承、模板布局

    ---------------------------------------------------------------------------------------------------- ...

  2. mysql数据库访问授权

    1.进入MySQL服务器 d:\mysql\bin\> mysql -h localhost -u root; 2.赋予任何主机访问数据的权限 mysql> GRANT ALL PRIVI ...

  3. JS异错面试题

    转自 http://www.codeceo.com/article/one-javascript-interview.html function Foo() { getName = function ...

  4. Django之权限用法

    **记住每一个url都是一个权限** 注册 可插拔试的权限,可以先写其他的逻辑,在最后再把权限加上 将rbac组件拷贝到项目上,注册项目 修改表结构 将写好的用户表对rbac的User表进行一对一的关 ...

  5. 数据分析R语言(1)

    无意中发现网上的一个数据分析R应用教程,看了几集感觉还不错,本文做一个学习笔记(知识点来源:视频内容+R实战+自己的理解),视频详细的信息请参考http://www.itao521.com/cours ...

  6. ERROR 2003 (HY000): Can't connect to MySQL server on 'ip地址' (110)

    用windows能远程连接数据库服务器,用ubuntu就报错,怎么都连不上,报这个错ERROR 2003 (HY000): Can't connect to MySQL server on 'ip地址 ...

  7. QCon2016 上海会议汇总(2) - 团队管理

    QCon 2016上海日程:http://2016.qconshanghai.com/schedule <当你的团队还支撑不起梦想时> - 链尚网技术合伙人 杨荣伟 Figo讲述了如何训练 ...

  8. 如何让Jackson JSON生成的数据包含的中文以unicode方式编码

    我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...

  9. [原创]java WEB学习笔记29:Cookie Demo 之自动登录

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. [原创]关于在CentOS 7.0 下 安装nfs ,遇见 Transaction check error问题的解决

    今天小弟的同学在使用阿里云的服务器安装nfs的时候,出现了一下问题 Transaction check error: file /usr/lib/systemd/system/blk-availabi ...