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
Output
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
这道题属于入门级DP,从下往上计算比较方便,递推公式F[i][j]=max{F[i+1][j],F[i+1][j+1]}+num[i][j],初始把最后一行数据赋给F数组,或者直接在num数组上计算。。。
Talk is cheap>>
package poj.dp; import java.util.Scanner; /**
* Created with IntelliJ IDEA.
* User: Blank
* Date: 2015/4/2
* Time: 20:42
*/
public class TheTriangle {
public static void main(String[] sure) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] num = new int[n][n];
int[][] cal = new int[n][n];
int i = 0;
while (i < n) {
int j = 0;
while (j <= i) {
num[i][j] = sc.nextInt();
j++;
}
i++;
}
for (int j = 0; j < n; j++) {
cal[n-1][j]=num[n-1][j];
}
for (int j = n - 2; j >= 0; j--) {
for (int k = n - 2; k >= 0; k--) {
cal[j][k]=Math.max(cal[j+1][k],cal[j+1][k+1])+num[j][k];
}
}
System.out.println(cal[0][0]);
}
}
POJ1163——The Triangle的更多相关文章
- POJ1163 The Triangle 【DP】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36918 Accepted: 22117 De ...
- (数字三角形)POJ1163 The Triangle
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59698 Accepted: 35792 De ...
- POJ1163 The Triangle
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44997 Accepted: 27174 Description 73 ...
- 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 pro ...
- poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)
经典的数塔模型. 动态转移方程: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...
- POJ1163 The Triangle: 倒三角形问题
经典的DP问题,DP思想也很直接: 直接贴代码: #include<iostream> #include<cstdio> #include<algorithm> # ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- jni那点事
今天说一下在jni开发中常另新手迷惑的JNIEnv这个东西 比如一个c语言的函数 JNIEXPORT jstring JNICALL Java_com_mmmmar_nativethread_Main ...
- C#读取Word文档内容代码
首先要添加引用com组件:然后引用: using Word = Microsoft.Office.Interop.Word; 获取内容: /// /// 读取 word文档 返回内容 /// //// ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
- springmvc常见注解模式
常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...
- (转)PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
在看帝国cms的connect.php是发现第一句是error_reporting(E_ALL ^ E_NOTICE);以前也没注意过这个语句,知道是设置错误提示的,但不清楚具体怎样设置使用.下面从网 ...
- Swift - 12 - 区间运算符和for-in
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- js删除数组指定的某个元素
1.给js数组对象原型加indexof方法 获得元素索引 Array.prototype.indexOf = function(val) { for (var i = 0; i < this.l ...
- centos 6.X 安装node
1.源码安装 Source Code yum -y install gcc make gcc-c++ openssl-devel wget //yum下载相关的依赖包 wget http://node ...
- 【USACO 3.1.2】总分
[描述] 学生在我们USACO的竞赛中的得分越多我们越高兴.我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助.我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是 ...
- ar命令和nm命令(建库!)
ar 命令详解 今天,跟着我们的技术大牛学了不少东西,首先就是这个ar命令啦. 当我们的程序中有经常使用的模块,而且这些模块在其他程序中也会用到,为了实现代码重用减少软件开发周期,我们可以将它们生成库 ...