看题传送门门:http://poj.org/problem?id=1163

困死了。。。。QAQ

普通做法,从下往上,可得状态转移方程为:

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

#include<cstdio>
#include<cstring>
int a[101][101];
int dp[101][101]; int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
scanf("%d",&a[i][j]); for(int i=1;i<=n;i++)
dp[n][i]=a[n][i]; for(int i=n-1;i>=1;i--)
{
for(int j=1;j<=i;j++)
dp[i][j]= a[i][j] + (dp[i+1][j] > dp[i+1][j+1]? dp[i+1][j]:dp[i+1][j+1]);
} printf("%d\n",dp[1][1]);
}
}

记忆化搜索,本题数据量小,与上面的都是0ms,但记忆化搜索保证每个子结点只访问一次,速度应该更快。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[101][101];
int dp[101][101];
int n;
int d(int i,int j)
{
if(dp[i][j]>=0)
return dp[i][j]; return dp[i][j]= a[i][j] + (i==n ? 0: max ( d(i+1,j) ,d(i+1,j+1)));
} int main()
{
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(dp,-1,sizeof(dp)); for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
scanf("%d",&a[i][j]); d(1,1); printf("%d\n",dp[1][1]);
}
}

尝试使用宏定义让程序变得简洁而优雅

#include<cstdio>
#include<cstring>
#define F(i,n) for(int i=1;i<=n;i++)
int a[101][101];
int dp[101][101];
int n; int main()
{
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(dp,-1,sizeof(dp)); F(i,n)
F(j,i)
scanf("%d",&a[i][j]); F(i,n)
dp[n][i]=a[n][i]; for(int i=n-1;i>=1;i--)
{
F(j,i)
dp[i][j]= a[i][j]+ (dp[i+1][j] > dp[i+1][j+1]? dp[i+1][j]:dp[i+1][j+1]);
} printf("%d\n",dp[1][1]);
}
}

最新文章

  1. Sqli-LABS通关笔录-12
  2. Boot loader: Grub进阶[转]
  3. Python Django 的 django templatedoesnotexist
  4. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON SetLineStyle2
  5. Contoso 大学 - 10 - 高级 EF 应用场景
  6. hdu 1402 A * B Problem Plus FFT
  7. Leetcode题解(二)
  8. Nginx实现负载均衡的几种方式
  9. 编译varnish 报No package &#39;libpcre&#39; found
  10. 1100C NN and the Optical Illusion
  11. go语言练习:数组
  12. installers PHPManager
  13. iOS开发中的压缩以及解压
  14. error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct
  15. JS获取对象“属性”的方法
  16. c#去除DataTable空列
  17. p-value值的认识
  18. vue路由页面加载的几种方法~
  19. yield学习
  20. 论文笔记 — L2-Net: Deep Learning of Discriminative Patch Descriptor in Euclidean Space

热门文章

  1. 给网站设置ICO图标
  2. wpf app全局变量传参方法(代码片段 )
  3. [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
  4. 批量删除Windows7中隧道适配器的方法
  5. WIN8.1的安装和打开"这台电脑"速度很慢的解决办法
  6. 至顶网推荐-Rpm另类用法加固Linux安全
  7. golang sync.Mutex(2)
  8. Codeforces Round #195 (Div. 2) 少部分题解
  9. CISP/CISA 每日一题 四
  10. 上传excel数据到数据库中