POJ1163——The Triangle

Description

73   88   1   02   7   4   44   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]);
    }
}
时间: 2024-08-09 10:44:56

POJ1163——The Triangle的相关文章

POJ1163 The Triangle 【DP】

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 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

poj1163 The Triangle

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37931   Accepted: 22779 Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on

【动态规划初级】动态规划入门 poj1163 The Triangle

动态规划 我在学习算法的时候,就被动态规划搞得是一头雾水,这几日终于是弄明白是怎么回来. 明白之后我才发觉我以前就碰到过一道ACM题,大意是这样的: 有这样形式的一种排列: 例如: 7      3   8    8   1   0  2   7   4   44   5   2   6   5从顶至下找一条路径,使得这条路径上的数字之和最大,而且每一步只能向左下或右下找,直到到最后一行. 比如:第二行的3只能找第三行8或者1. 上面例子的最大一条路径是:7-3-8-7-5;总和30(原题:ht

POJ1163 The Triangle: 倒三角形问题

经典的DP问题,DP思想也很直接: 直接贴代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 const int max_size=1001; 7 int n, a[max_size][max_size]; 8 int f[3][max_size]; 9 void initiate(){ 1

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

经典的数塔模型. 动态转移方程:  dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+p[i][j]; 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in