POJ 3176 : Triangle - 水DP
来源:互联网 发布:淘宝买家秀福利的店铺 编辑:程序博客网 时间:2024/05/19 13:21
额,算是完全我自个儿敲出的第一道DP了吧……好水的DP~瞬间有那么点信心了?同学报告里的第一个题。
题意:
拿图说话,题意看图便好理解了。
Explanation of the sample:
7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5
这样走能够得到最大和:30.
分析:
主要就两点,
1.每行首尾2个的d[i][j]值 是完全确定的,因其只能由首尾唯一一个前躯得到;
2.其它每个数选择(由肩上2点)两种转移中大者即可。
状态转移方程即为d[i][j]=max{d[i-1][j]+a[i][j], d[i-1][j]};
真的很水!~~~~~
3176Accepted1624K63MSG++552B2014-03-28 15:04:26
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[355][355],d[355][355];int main(){int n,i,j,ans;while(~scanf("%d",&n)){for(i=1;i<=n;i++)for(j=1;j<=i;j++)scanf("%d",&a[i][j]);d[1][1]=a[1][1];for(i=2;i<=n;i++){d[i][1]=d[i-1][1]+a[i][1];d[i][i]=d[i-1][i-1]+a[i][i];for(j=2;j<i;j++){d[i][j]=max(d[i-1][j-1],d[i-1][j]);d[i][j]+=a[i][j];}}ans=-1;for(i=1;i<=n;i++)if(ans<d[n][i])ans=d[n][i];printf("%d\n",ans);}return 0;}
0 0
- POJ 3176 : Triangle - 水DP
- POJ 1163&& 3176 The Triangle(DP)
- poj 1163The Triangle(水dp)
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
- POJ 1163 The Triangle (DP)
- POJ 1163 The Triangle【DP】
- poj 1163 The Triangle【dp】
- [DP] POJ 1163 The Triangle
- 【DP】 POJ 1085 Triangle War
- poj 1163__The Triangle(dp)
- POJ 1163 The Triangle DP
- poj 1163 The Triangle --dp
- POJ 1163 The Triangle [DP]
- poj 1163 The Triangle 基础DP
- POJ 1163 The Triangle 简单DP
- DP专题1 POJ 1163 The Triangle
- POJ 1163 The Triangle 简单DP
- POJ 1163 The Triangle (简单DP)
- POJ 3177 / POJ 3352 : Redundant Paths / Road Construction - 边双连通分量,缩点
- #include<list>类的用法
- js DateFormat
- 带分数 - 搜索,next_permutaion妙用
- POJ 1276 : Cash Machine - 多重背包
- POJ 3176 : Triangle - 水DP
- POJ 2533 : Longest Ordered Subsequence - LIS问题
- POJ 1631 : Bridging signals - 最长上升子序列 O(nlog n)
- ZOJ 3770 : Ranking System (第14届浙江大学程序设计竞赛 D) - 结构体排序,vector
- POJ 1966 / ZOJ 2182 : Cable TV Network - 顶点连通度
- POJ 1062 : 昂贵的聘礼 - 最短路Dijkstra+枚举(难)
- POJ 1988 : Cube Stacking - 简单并查集
- POJ 1060/ ZOJ 1026 : Modular Multiplication of Polynomials - f(x)g(x) mod h(x)
- POJ 2246/ ZOJ 1094 : Matrix Chain Multiplication - 矩阵相乘,模拟