一、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. Android笔记之GridView

    完整Demo链接:https://pan.baidu.com/s/1d_G9aCwBxpiYQcdQhwSDDw,提取码:5deh 效果图 activity_main.xml <?xml ver ...

  2. openstack 官方镜像qcow2 下载和修改密码

    下载地址: CentOS6:http://cloud.centos.org/centos/6/images/ CentOS7:http://cloud.centos.org/centos/7/imag ...

  3. Power Designer体验之旅

    版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/wang13667539325/article/details/36025245 从某种程度上说.不论 ...

  4. Android Studio 1.1 使用介绍及导入 jar 包和第三方依赖库

    导入 jar 包 导入 jar 包的方式非常简单,就是在项目中的 libs 中放入你需要导入的 jar 包,然后右键你的 jar 文件,选择“add as a library”即可在你的项目中使用这个 ...

  5. linux 6-find,xargs

    十六. 文件查找命令find:     下面给出find命令的主要应用示例:     /> ls -l     #列出当前目录下所包含的测试文件     -rw-r--r--. 1 root r ...

  6. let和var以及const有什么区别

    在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. for(var i=0;i&l ...

  7. ELK日志分析系统-Logstack

    ELK日志分析系统 作者:Danbo 2016-*-* 本文是学习笔记,参考ELK Stack中文指南,链接:https://www.gitbook.com/book/chenryn/kibana-g ...

  8. ios中文模糊搜索兼容问题

    $(function(){ var cpLock = true; $("input[name='name']").off().on({ compositionstart: func ...

  9. Referrer-Policy常见属性

    Referrer-Policy(来源协议)用来规定什么情况下显示Referer字段及refer字段内显示多少信息. 备注: referer实际上是对referrer的误写,因为写错的人多了也就正确了. ...

  10. dbgrid,datasoure,ClientDataSet的简单应用

    dbgrid是用来在界面上显示数据的,需要连接源dbgrid1.datasource := datasource1; datasource:作为dbgrid,clientDataset的连接桥梁,需要 ...