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. vue-template-compiler作用

    vue-template-compiler的作用是什么: 看起来 template-compiler是给parse函数使用的, 那么parse函数是干什么的呢 先看一下parse的结果: 结论:使用v ...

  2. 小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构

    笔记 第二章 架构演进和分布式系统基础知识 1.传统架构演进到分布式架构     简介:讲解单机应用和分布式应用架构演进基础知识 (画图) 高可用 LVS+keepalive :负载均衡的知识点 1. ...

  3. 六十六:CSRF攻击与防御之CSRF防御之ajax防御和ajax封装

    app里面还是要绑定CSRFProtect from flask_wtf import CSRFProtect # flask_wtf 已经提供CSRF的防御手段CSRFProtect(app) # ...

  4. Python中的变量和作用域详解

    Python中的变量和作用域详解 python中的作用域分4种情况: L:local,局部作用域,即函数中定义的变量: E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局部 ...

  5. Sublime Text3学习参考集

    1.如何优雅地使用Sublime Text: http://jeffjade.com/2015/12/15/2015-04-17-toss-sublime-text/#three 持续更新中..... ...

  6. jdk1.8-ArrayDeque

    一:类的继承关系 UML图 类的继承关系: )))))) ]) & ()) == ) & ()) == ) & ()] = e) ) & (); return resu ...

  7. php进阶之路 -- 03 命名空间

    php进阶之路 -- 03 命名空间 命名空间概述 定义命名空间 空间成员和子空间 空间成员的访问 空间引入 全局空间 一. 命名空间概述 什么是命名空间?从广义上来说,命名空间是一种封装事物的方法. ...

  8. keys随机生成

    随机keys生成 const fs=require('fs'); const KEY_LEN=1024; const KEY_COUNT=2048; const CHARS='abcdefghijkl ...

  9. 华为HCNA乱学Round 8:生成树

  10. SolidWorks学习笔记4特征

    绘制斜的拉伸效果 一般拉伸方向垂直于草图基准面, 可以实现绘制一条线,作为其拉伸方向 效果如下 简单孔 在菜单中选择“插入”--“特征”---“简单直孔” 选择一个平面放置 设置好孔的直径和深度后,确 ...