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 ...
随机推荐
- Android studio2 中的 SDK Manager的使用-------Android SDK 的安装与更新(Install missing platform(s) and sync project 编译错误解决)
最近在编写Android程序,其中有一个问题就是对旧应用的导入,此时往往你的Android SDK中并没有老版本的Android SDK, 此时往往会提示你出现错误 Install missing p ...
- idea 错误: 找不到或无法加载主类 xxx.xxx.xxxxx
idea 错误: 找不到或无法加载主类 xxx.xxx.xxxxx JDK环境,maven项目还是ee还是web项目,是否都正常. 如果是用idea打开的话,在源码目录上点击右键,然后找到Mark d ...
- HBase 数据恢复
参考链接: https://community.hortonworks.com/content/supportkb/48748/hbase-master-wont-start-with-followi ...
- Web测试方法_02
1.页面链接检查 检查每一个链接是否都有对应的页面,页面与页面之间的来回切换是否正常响应,包括一些返回页面的链接是否正常,还要检查点击图片所链接的页面是否准确展示. 2.相关性检查 功能相关性检查:例 ...
- phpstorm 远程连接服务器进行开发
phpstorm phpstorm是一款功能强大的ide编辑器,有了它,你敲代码速度能比用notepad++快数倍(初学者不建议使用ide,建议使用notepad++),缺点是没有固态的电脑带起来吃力 ...
- PJzhang:shell基础入门的2个疗程-one
猫宁!!! 在centos7上操作这一切 第1节:什么是shell centos7默认使用shell的bash cat /etc/shells 第2节:linux的启动过程 BIOS(主板,引导介质) ...
- Python面试-DB相关
昨日回顾: 面试 Python综述 设计哲学 版本变迁及发展 GIL 内存管理及垃圾回收 并发并行 昨日作业一:Python是否存在内存泄露 作业二:过往的项目中有没有出现过性能问题? 作业三:什么是 ...
- VIM全总结
Vim是Linux自带的编辑器,是Vi的高级版,刚开始使用我是一脸懵逼的,跟普通的编辑器完全不同,基本全靠键盘,写完之后也不知道怎么保存,特地花了点时间了解了一下,还是挺简单的.其中,主要包含三种模式 ...
- hdfs(分布式文件系统)优缺点
hdfs(分布式文件系统) 优点 支持超大文件 支持超大文件.超大文件在这里指的是几百M,几百GB,甚至几TB大小的文件.一般来说hadoop的文件系统会存储TB级别或者PB级别的数据.所以在企业的应 ...
- sqlalchemy链接数据库
from sqlalchemy import create_engine HOSTNAME = '127.0.0.1' PORT = 3306 DATABASE = 'first_sqlalchemy ...