POJ 3176 : Triangle - 水DP
2015-01-10 21:46
295 查看
额,算是完全我自个儿敲出的第一道DP了吧……好水的DP~瞬间有那么点信心了?同学报告里的第一个题。
题意:
拿图说话,题意看图便好理解了。
Explanation of the sample:
分析:
主要就两点,
1.每行首尾2个的d[i][j]值 是完全确定的,因其只能由首尾唯一一个前躯得到;
2.其它每个数选择(由肩上2点)两种转移中大者即可。
状态转移方程即为d[i][j]=max{d[i-1][j]+a[i][j], d[i-1][j]};
真的很水!~~~~~
#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
[i])
ans=d
[i];
printf("%d\n",ans);
}
return 0;
}
题意:
拿图说话,题意看图便好理解了。
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]};
真的很水!~~~~~
3176 | Accepted | 1624K | 63MS | G++ | 552B | 2014-03-28 15:04:26 |
#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
[i])
ans=d
[i];
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- poj-3176 Cow Bowling &&poj-1163 The Triangle && hihocoder #1037 : 数字三角形 (基础dp)
- ACM POJ 1163 The Triangle DP
- poj 1163 The Triangle 因为这个,发现DP很有意思
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
- DP--数字三角形 (POJ 3176 && hihoCoder 1037)
- poj 1163/3176 数字三角形【简单DP】
- hdu(poj)-2084(3176)-数塔-简单dp
- poj1163 dp the triangle
- POJ 1163 The Triangle DP 逐点累加
- poj 3176 dp
- POJ 3176 Cow Bowling (简单dp——数塔问题)
- poj 3176 Cow Bowling 数塔dp
- DP-POJ 1163 Triangle(简单数字三角形)
- poj 3176 (dp 金字塔)
- poj 3176 -- Cow Bowling (dp)
- POJ 3176-Cow Bowling(DP||记忆化搜索)
- poj 1163 The Triangle &poj 3176 Cow Bowling (dp)
- POJ 1163 The Triangle & POJ 3176 Cow Bowling(DP动态规划)
- POJ 3176 Cow Bowling(基础DP)
- POJ-3176 Cow Bowling【基础DP】