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. [VBA]定向提取符合条件的内容

    要求:若A列满足值为c,则将b列对应的内容背景色调为红色,并提取出来: 代码如下: Sub naqu()Dim i As IntegerFor i = 2 To Range("a65536& ...

  2. WCF绑定(Binding)

    一个Binding由一个有序的binding元素栈所组成,其中的每一个元素都指定了连接到ServiceEndpoint的一个方面.在这个栈中的最底两层都是必须要有的.最底下的一层是传输binding元 ...

  3. Selenium 2自动化测试实战25(自动化测试模型)

    一.自动化测试模型 自动化测试模型介绍:线性测试.模块化驱动测试.数据驱动测试和关键字驱动测试 线性测试:每个测试脚本相对独立,且不产生其他依赖与调用,只是单纯的来模拟用户完整的操作场景.模块化驱动测 ...

  4. pyqt5 工具栏文字图片同时显示

    import sys from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication from PyQt5.QtGu ...

  5. MySQL数据库5.7全文索引的坑

    1.引擎必须是MyIsAm 2.创建全文索引:ALTER TABLE articles ADD FULLTEXT (title,body); 3.注意全文搜索的字段必须等于或者大于4个字节才会有效 4 ...

  6. Spring boot (一):入门篇

    Spring boot 简介 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...

  7. 实现文件上传功能(FileUpload组件)

    文件上传: 项目中经常用到文件上传. 自己实现文件上传,使用文件上传组件fileupload组件 1.指定表单类型为文件上传, enctype=”multipart/form-data” 2.提交方式 ...

  8. Star all over again.

    0x00前言 经过了一上午的折腾之后,博客的界面勉强可观,今天下午将之前的所有博客全部删除,重新开始写属于自己的博客,而不是只把它当作一个收藏夹,转载其他人的文章. 0x01近来感想 有感而发,随便写 ...

  9. 【Python开发】使用pyplot模块绘图

    快速绘图 使用pyplot模块绘图¶ matplotlib的pyplot模块提供了和MATLAB类似的绘图API,方便用户快速绘制二维图表.我们先看一个简单的例子: 05-matplotlib/mat ...

  10. 解决网页ICON图标无法显示的问题

    第一步:检查下“<link rel="shortcut icon" href="http://www.bhcode.net/favicon.ico" ty ...