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,从下往上计算比较方便,递推公式F[i][j]=max{F[i+1][j],F[i+1][j+1]}+num[i][j],初始把最后一行数据赋给F数组,或者直接在num数组上计算。。。

Talk is cheap>>

package poj.dp;

import java.util.Scanner;

/**
* Created with IntelliJ IDEA.
* User: Blank
* Date: 2015/4/2
* Time: 20:42
*/
public class TheTriangle {
public static void main(String[] sure) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] num = new int[n][n];
int[][] cal = new int[n][n];
int i = 0;
while (i < n) {
int j = 0;
while (j <= i) {
num[i][j] = sc.nextInt();
j++;
}
i++;
}
for (int j = 0; j < n; j++) {
cal[n-1][j]=num[n-1][j];
}
for (int j = n - 2; j >= 0; j--) {
for (int k = n - 2; k >= 0; k--) {
cal[j][k]=Math.max(cal[j+1][k],cal[j+1][k+1])+num[j][k];
}
}
System.out.println(cal[0][0]);
}
}

POJ1163——The Triangle的更多相关文章

  1. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  2. (数字三角形)POJ1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59698   Accepted: 35792 De ...

  3. POJ1163 The Triangle

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44997   Accepted: 27174 Description 73 ...

  4. Poj1163 The Triangle(动态规划求最大权值的路径)

    一.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 pro ...

  5. poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)

    经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; #include <iostream> #include ...

  6. POJ1163 The Triangle: 倒三角形问题

    经典的DP问题,DP思想也很直接: 直接贴代码: #include<iostream> #include<cstdio> #include<algorithm> # ...

  7. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  9. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

随机推荐

  1. 在cmd窗口下运行Java程序时无法找到主类的解决办法

    我是Java的初学者,昨天在cmd窗口下运行一段Java程序时总是有问题,可以编译但无法执行. 也就是javac时正确,一旦java时就不对了,提示找不到或无法加载主类,经百度谷歌再加上自己的摸索终于 ...

  2. git 创建远程仓库

    在远程服务器上$ cd /server/path/ $ git init --bare myproject.git 在本地 1> $ cd /client/path/ 运行 git init 2 ...

  3. 自己编写SqlHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  4. C#反射—解决类的实例化问题

    利用简单工厂模式来改进抽象工厂使用的复杂性(抽象工厂详见 设计模式之—抽象工厂模式) 数据表(User)业务处理接口(IUser) namespace FactoryMethodPatternDB.C ...

  5. 2015 Multi-University Training Contest 1 题解 BY FZUw

    题目链接:5288-5299 HDU5288 题解原文链接:我是链接

  6. 【翻译】使用nginx作为反向代理服务器,uWSGI作为应用服务器来部署flask应用

    最近在看关于Docker和Nginx方面的内容,先于在Docker上开发以及部署python应用自然要先能够在本机上部署,其中找到一篇文章写的最为详细并且实验成功,所以在此翻译转载过来以备后需.[原文 ...

  7. dbms_job dbms_scheduler简单比较

    ---------------------------陈旧的-------------------------------------/*--------------------- 创建job --- ...

  8. [转] 关于UIView

    [转载] 原文地址 :http://blog.csdn.net/itianyi/article/details/8982518 UIView是开发中使用得最多的控件了,深入的理解很有必要. UIVie ...

  9. SVN的使用(转发)

    http://my.oschina.net/joanfen/blog/194491?fromerr=LM5QY3YF

  10. 1.Weblogic通Eclipse调试配置(Weblogic同Eclipse调试配置技术)

    概述:环境是eclipse,maven,svn, 在实际的的应用项目中,我们经常遇到本地应用程序没有问题,而部署到Weblogic上缺出现问题,查看日志并找不到原因,这时就需要调试部署上的程序与本地e ...