The Triangle
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

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.

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)的更多相关文章

  1. POJ 1163 The Triangle DP题解

    寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...

  2. poj 1163 The Triangle &amp;poj 3176 Cow Bowling (dp)

    id=1163">链接:poj 1163 题意:输入一个n层的三角形.第i层有i个数,求从第1层到第n层的全部路线中.权值之和最大的路线. 规定:第i层的某个数仅仅能连线走到第i+1层 ...

  3. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  4. POJ 1163 The Triangle【dp+杨辉三角加强版(递归)】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49955   Accepted: 30177 De ...

  5. POJ 1163 The Triangle 简单DP

    看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...

  6. 递推DP POJ 1163 The Triangle

    题目传送门 题意:找一条从顶部到底部的一条路径,往左下或右下走,使得经过的数字和最大. 分析:递推的经典题目,自底向上递推.当状态保存在a[n][j]时可省去dp数组,空间可优化. 代码1: /*** ...

  7. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  8. poj 1163 The Triangle 记忆化搜索

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44998   Accepted: 27175 De ...

  9. POJ - 1163 The Triangle 【动态规划】

    一.题目 The Triangle 二.分析 动态规划入门题. 状态转移方程$$DP[i][j] = A[i][j] + max(DP[i-1][j], DP[i][j])$$ 三.AC代码 1 #i ...

随机推荐

  1. Centos7 Devstack [Rocky] 重启后无法联网

    部署devstack-rocky版本后网络,可以 Ping 通自己的 IP,但 Ping 不同网关,ping不通同网段主机,查看网卡和ovs信息如下 解决 第一步 按造网上教程,修改br-ex,ens ...

  2. oracle imp 工具可能出现的问题

  3. 进程,虚拟环境,Mysql主从

    进程 查看进程 ps(类似windows任务管理器) man 1 ps # 查看命令文档 ps[options] 1 UNIX options, which may be grouped and mu ...

  4. PHP架构剖析

    一:PHP是什么 PHP("PHP: Hypertext Preprocessor",超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML中 ...

  5. 类属性与对象实现,init方法的作用,绑定方法,绑定方法与普通函数的区别,继承,抽象与继承,派生与覆盖

    今日内容: 1.类属性与对象属性 2.init方法的作用 3.绑定方法 4.绑定方法与普通函数的区别(非绑定方法) 5.继承 6.抽象与继承 7.派生与覆盖 1.类属性与对象属性 类中应该进存储所有对 ...

  6. 【VS开发】获得devcon.exe

    1.获得devcon.exe 有两种方法,一是直接去网上下,不过下的很多64位的都不能用,二是自己装个ddk去安装目录下找,在WinDDK\7600.16385.1\tools\devcon下,当然还 ...

  7. div随着屏幕滚动而滚动

    <script type="text/javascript"> $(document).ready(function () { var menuYloc = $(&qu ...

  8. MYSQL中的UNION和UNION ALL

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...

  9. RabbitMQ 示例-生产者-消费者-direct-topic-fanout

    这是生产者和消费者2个项目, 包含 direct,topic,fanout模式下的消费,springboot + rabbitmq 代码地址:https://github.com/duende99/R ...

  10. CentOS7实现Nginx+Tomcat 负载均衡

    0. 说明 将nginx作为负载均衡器,反向代理,tomcat作为节点服务器 192.168.100.201:nginx服务器 192.168.100.202:Tomcat 1服务器 192.168. ...