POJ1163 The Triangle
来源:互联网 发布:七星彩庄家统计软件 编辑:程序博客网 时间:2024/05/21 17:05
DP最最基础的题型:数字塔问题
题意 有一个n层逐层个数递加的三角数字塔,从塔顶开始向下走,每次只能向左半格或向右半格走,问走到塔底的路程上的数的和最大是多少。
7 3 8 8 1 0 2 7 4 44 5 2 6 5
这时,7→3→8→7→5最大,为30.输入样例
5(n)
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
输出样例
30
思路 这是一道DP基础题。不难看出,到每个点为止的最优解,就是这个点的值加上其左下方和右下方的最大值。最下面的一行的最优解就是其本身,由此一行一行向上推,直到推到最上面的那个点,也就是答案了。
代码如下
//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ那些年错过的大雨、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ全部还给出题的你、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、田神AK专场 签到专用、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ//ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽ、ヽヽ、ヽ、ヽ#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <sstream>#include <cmath>#include <cctype>#include <stack>//栈#include <queue>//队列#include <set>//集合#include <map>//映射#include <vector>//不定长数组#include <algorithm>using namespace std;#define ll long longconst double pi = 4*atan(1.0);int gcd(int a,int b) {return b == 0? a:gcd(b,a%b);}int lcm(int a,int b) {return a/gcd(a,b)*b;}const int maxn = 110;int dp[maxn][maxn];int main(){ int n; scanf("%d",&n);//n层,第几层就有几个数 for(int i = 1 ; i <= n ; i ++){//i循环行,j循环列 for(int j = 1 ; j <= i ; j ++) scanf("%d",&dp[i][j]);//这里从1开始使用数组会比较方便,而且便于理解 } for(int i = n-1 ; i >= 1 ; i --){//i循环行(从下往上) j循环列 for(int j = 1 ; j <= i ; j ++) dp[i][j] += max(dp[i+1][j],dp[i+1][j+1]);//到[i][j]为止的最优解就是[i+1][j]的最优解和[i+1][j+1]的最优解的最大值再加上[i][j]值本身 } printf("%d\n",dp[1][1]);//一直推到最上面,自然就是最优解了 return 0;}
0 0
- POJ1163 The Triangle
- POJ1163:The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle 【DP】
- POJ1163 The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle
- POJ1163 The Triangle
- poj1163 the triangle 题解
- POJ1163 The Triangle
- POJ1163 The Triangle 题解
- POJ1163 The Triangle
- poj1163 dp the triangle
- POJ1163 The Triangle
- poj1163 The Triangle
- poj1163 The Triangle
- The Triangle(poj1163)
- POJ1163 The Triangle【DFS】
- 16周(oj 倒序)
- 数据库约束性
- Qt_OpenGL:光源小测
- 14分析1
- 第十六周OJ项目3相同数字!
- POJ1163 The Triangle
- 黑马程序员------java面向对象、多态
- 第16周项目4—为动态数组扩容
- Cocos2d-x中libcurl库的使用(2)curl_easy API的介绍
- /etc/init.d/rcS内容分析
- 【Android安全】Android软件保护措施总结
- Mongodb数据的备份与恢复
- PreferenceActivity简单使用app设置页
- 异常结构体EXCEPTION_RECORD structure