poj 1163 The Triangle (动态规划)

The Triangle

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37778   Accepted: 22685

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

Source

IOI 1994

动态规划基础题,主要掌握这种层层递推的思想,由最后一层往上递推;

题意就是从顶层的数字开始,有两个方向开始走,走到底层,输出最大的距离;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=101;
int dp[maxn][maxn],a[maxn][maxn];
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)
        scanf("%d",&a[i][j]);
    for(i=1;i<=n;i++)
        dp[n][i]=a[n][i];
    for(i=n-1;i>=0;i--)
        for(j=1;j<=i;j++)
        dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+a[i][j];//往上递推
    printf("%d\n",dp[1][1]);
    return 0;
}

还有一种节约内存的写法;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=101;
int a[maxn][maxn];
int *dp;
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)
        scanf("%d",&a[i][j]);
        dp=a[n];
    for(i=n-1;i>=0;i--)
        for(j=1;j<=i;j++)
        dp[j]=max(dp[j],dp[j+1])+a[i][j];
    printf("%d\n",dp[1]);
    return 0;
}
时间: 2024-12-28 20:26:48

poj 1163 The Triangle (动态规划)的相关文章

POJ 1163 The Triangle DP题解

寻找路径,动态规划法题解. 本题和Leetcode的triangle题目差不多一样的,本题要求的是找到最大路径和. 逆向思维,从底往上查找起就可以了. 因为从上往下可以扩展到很多路径,而从下往上个点的路径是由两条缩减到一条. 这样就可以很简单记录最大路径了. #include <stdio.h> const short MAX_ROW = 101; short triangle[MAX_ROW][MAX_ROW]; short table[MAX_ROW]; short row; inline

递推DP POJ 1163 The Triangle

题目传送门 1 /* 2 数塔 3 自底向上 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const

poj 1163 The Triangle &amp;poj 3167 Cow Bowling (dp)

链接:poj 1163 题意:输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个. 状态方程:f[i][j]=max(f[i-1][j-1],f[i-1][j])+a[i][j]; 1163代码: #include<stdio.h> #include<string.h> int a[105][105],f[105][105]; int max(int a,int b)

POJ 1163 The Triangle (简单线性dp)

OJ题目 : click here~~ 题目分析:给一个数字三角形,从最上面一个数字开始,方向只能往左下或者右下,一直到最后一行,求经过的所有数字和的最大值. 搞清楚在输入的数据中,route的方向就行. AC_CODE int num[102][102]; int main(){ int n , i , j , k ; while(cin >> n){ int x[102][102]; for(i = 1;i <= n;i++) for(j = 1;j <= i;j++) sca

POJ 1163 The Triangle

题目链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39022   Accepted: 23430 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 calculat

POJ 1163 The Triangle(经典问题教你彻底理解动归思想)

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

Poj 1163 The Triangle 之解题报告

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 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 t

POJ 1163 The Triangle(三种搜索方式)

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 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 e

poj 1163 The Triangle(dp)

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