poj 1163 The Triangle(dp)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 43993 | Accepted: 26553 |
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题,水过,纪念一下~
Java AC 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int input[][] = new int[rows + 1][rows + 1];
int result[][] = new int[rows + 1][rows + 1]; //将每个点的最大结果存在数组里
for(int i = 1; i <= rows; i++)
for( int j = 1; j <= i; j++) {
input[i][j] = sc.nextInt();
}
result[1][1] = input[1][1];
dp(input, result);
int max = Integer.MIN_VALUE;
for(int i = 1; i <= rows; i++) { //找出最后一行最大的一个,即为结果
if(result[rows][i] > max)
max = result[rows][i];
}
System.out.println(max);
}
public static void dp(int[][] input, int[][] result) {
int rows = input.length - 1;
for(int i = 2; i <= rows; i++)
for(int j = 1; j <= i; j++) {
if(j == 1) //每行的第一列的最大和 只能由上一行的第一列的最大和得到
result[i][j] = result[i - 1][j] + input[i][j];
else if(j == i) //每行的最后一列的最大和 只能由上一行的最后一列的最大和得到
result[i][j] = result[i - 1][j - 1] + input[i][j];
else //其他的则是可以由两个方向中大的那个得到
result[i][j] = Math.max(result[i - 1][j - 1], result[i - 1][j]) + input[i][j];
}
}
}
poj 1163 The Triangle(dp)的更多相关文章
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49955 Accepted: 30177 De ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- 递推DP POJ 1163 The Triangle
题目传送门 题意:找一条从顶部到底部的一条路径,往左下或右下走,使得经过的数字和最大. 分析:递推的经典题目,自底向上递推.当状态保存在a[n][j]时可省去dp数组,空间可优化. 代码1: /*** ...
- OpenJudge/Poj 1163 The Triangle
1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...
- poj 1163 The Triangle 记忆化搜索
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44998 Accepted: 27175 De ...
- POJ - 1163 The Triangle 【动态规划】
一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...
随机推荐
- python3.6安装jpype1后引入jpype报“ImportError: numpy.core.multiarray failed to import”问题
jpype是调用java接口的第三方库,通过该库,python可以运行java程序,从而解决一些调用java的问题,比如:java开发的接口,测试时, 有java的加密算法就不用python写一遍重复 ...
- Initializer for conditional binding must have Optional type, not 'String'
今天看到问Swift问题: Initializer for conditional binding must have Optional type, not 'String' 以前没遇到过这个问题, ...
- python去掉空格和 b
直接看下面实例: In [52]: output=subprocess.check_output(["head -c 16 /dev/urandom | od -An -t x | tr - ...
- MySQL乱码的原因和设置UTF8数据格式
https://segmentfault.com/a/1190000018662023 MySQL使用时,有一件很痛苦的事情肯定是结果乱码.将编码格式都设置为UTF8可以解决这个问题,我们今天来说下为 ...
- java中instanceof的基本使用
java中的instanceof运算符是用于判断对象是否是指定类或这个指定类的子类的一个实例,返回值是布尔类型. 语法: boolean result = object instanceof clas ...
- Linux中如何批量删除目录下文件后缀
1. rename rename分为perl版本和C版本,以下截图是C版本效果: perl版本:rename 's/.bak//' *.bak 2. for循环+awk 3. for循环+cut 4. ...
- windows客户端如果通过cmd窗口连接到远程linux服务器,可以使用telnet;
linux系统打开telnet端口的方法 2016-03-11 16:02:25 标签:linux telnet 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. ...
- 【MFC】BitBlt详解
设备上下文绘图有很多种方法.例如通过创建位图画刷,利用其填充一个区域来实现图像的绘制.此外,还可以使用CDC类的位图函数来输出位图到设备上下文中. BitBlt 用于从原设备中复制位图到目标设备,语法 ...
- JavaScript基础入门08
目录 JavaScript 基础入门08 DOM 介绍 绑定事件 给一组元素绑定事件 节点 节点树 节点类型 选取文档内容 通过id选取元素 通过指定的标签名选取元素 用指定的css类来选取元素 通过 ...
- Goland 开发插件安装
goland 是一款非常优秀的开发工具,默认打开后,发白的开发界面,也是异常刺眼.但是 Goland 为我们准备了很多插件,要优先安装这些插件,打造适合自己的开发界面. 我自己的设置的主题界面如下: ...