Poj1163 The Triangle(动态规划求最大权值的路径)
一、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
The numbers in the triangle, all integers, are between 0 and 99.
Output
二、问题分析
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(动态规划求最大权值的路径)的更多相关文章
- hdu 4293 dp求最大权值不重合区间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> # ...
- 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)
这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...
- 奔小康赚大钱 HDU - 2255(最大权值匹配 KM板题)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...
- POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of ...
- POJ 2400 Supervisor, Supervisee(KM二分图最大权值匹配)题解
题意:n个老板n个员工,先给你n*n的数据,i行j列代表第i个老板第j喜欢的员工是谁,再给你n*n的数据,i行j列代表第i个员工第j喜欢的老板是谁,如果匹配到第k喜欢的人就会产生一个分数k-1.现在让 ...
- hdu 1853 Cyclic Tour 最大权值匹配 全部点连成环的最小边权和
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) ...
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...
随机推荐
- [ArcGIS]Oracle RAC下创建地理数据库(Create Enterprise Geodatabase)失败的解决方法
转载请注明原文地址:http://www.cnblogs.com/litou/p/8028843.html 环境:Oracle 11g 11.2.0.1.0(双节点RAC群集),ArcGIS Desk ...
- 爬虫-【selenium—Webdriver元素定位的八种常用方式
在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素.其中By类的常用定位方式共八种,现分别介绍如下 ...
- Broadcast Intent & Broadcast Receiver
当Android系统发生某种状况,必须通知所有程序进行处理时,例如电量不足等,可利用Broadcast Intent对象的功能来进行信息广播. 运行机制包括两部:送出Intent对象的程序:监听广播信 ...
- angularjs 中的iframe 标签 ng-src 路径 z-index 必须有position
如果直接写路径到iframe标签里的ng-src中会出现报错: 解决方法: 1.ng里面有个属性是专门用来解决跨域问题的 $sce. 用法: $scope.someUrl = $sce.trustAs ...
- UDP标准模型
伪代码 #服务端 #创建UDP服务器 ss = socket() #创建一个服务器套接字 ss.bind() #绑定服务器套接字 inf_loop: #服务器无限循环 cs = ss.recvfrom ...
- Swift 闭包的简单学习
OC中已经学习了闭包 在swift里面 该怎么处理 不多说 上代码 //(num:Int) ->Bool是闭包的参数类型 func hasCloserMatch(arr :[Int], valu ...
- hd acm1017
Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...
- EntityFramework 学习 一 Lazy Loading
延迟加载:延迟加载相关的数据 using (var ctx = new SchoolDBEntities()) { //Loading students only IList<Student&g ...
- hdu 5475 线段树
An easy problem Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 从request中读数据流
ServletInputStream servletInputStream = reqeust.getInputStream(); int len=0; int size=reqeust.getCon ...