POJ 1163: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 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,直接记录状态

dp[i][j]表示以第i行第j列开头的三角形的最大值

dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+a[i][j];

(1) 从后向前递推

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 const int MAXN = 1001;
 5 int dp[MAXN][MAXN];
 6 int main()
 7 {
 8     int n;
 9     cin >> n;
10     for (int i = 0; i < n;i++)
11       for (int j = 0; j <= i; j++)
12         cin >> dp[i][j];
13     for (int i = n - 2; i >= 0;i--)
14       for (int j = 0; j <= i; j++)
15       {
16           dp[i][j] = max(dp[i+1][j],dp[i+1][j+1]) + dp[i][j];
17       }
18       cout << dp[0][0];
19     return 0;
20 }

(2) 从前向后递推

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 const int MAXN = 1001;
 5 int dp[MAXN][MAXN];
 6 int main()
 7 {
 8     int n;
 9     cin >> n;
10     for (int i = 0; i < n;i++)
11       for (int j = 0; j <= i; j++)
12         cin >> dp[i][j];
13     for (int i = 0; i < n;i++)
14       for (int j = 0; j <= i; j++)
15       {
16           if (i==0)continue;
17           else if (j == 0)dp[i][j] = dp[i-1][j] + dp[i][j];
18           else if (j == i)dp[i][j] = dp[i-1][j-1] + dp[i][j];
19           else dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + dp[i][j];
20       }
21       int ans = 0;
22       for (int j = 0; j < n; j++)
23           ans = max(ans,dp[n-1][j]);
24       cout << ans;
25     return 0;
26 }
时间: 2024-10-31 12:05:08

POJ 1163:The Triangle的相关文章

POJ 1163&amp;&amp; 3176 The Triangle(DP)

The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41169   Accepted: 24882 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 &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 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 3176:Cow Bowling

Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13464   Accepted: 8897 Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard

POJ 1963:All in All

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27707   Accepted: 11381 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1659:Frogs&#39; Neighborhood(Havel-Hakimi定理)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6898   Accepted: 3006   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..