题目链接:http://poj.org/problem?id=1163
其实个人感觉这道题可以算是记忆化深度优先搜索,也可以算是动态规划。如果认识上有错误请轻喷。。
和之前的某道题很相似,能做出这道题也算是自己的学习成果吧!
#include<cstdio>
#include<cstring>
using namespace std;
int s[101][101];
int mark[101][101];
int count(int i,int j)
{
int ans;
if(i==1&&j==1)
return s[1][1];
if(mark[j][i]!=-1)
return mark[j][i];
else
{
if(i==1)
ans=s[j][i]+count(i,j-1);
else if(i==j)
ans=s[j][i]+count(i-1,j-1);
else
{
if(count(i-1,j-1)>count(i,j-1))
ans=s[j][i]+count(i-1,j-1);
else
ans=s[j][i]+count(i,j-1);
}
}
mark[j][i]=ans;
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(s,0,sizeof(s));
memset(mark,-1,sizeof(mark));
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
scanf("%d",&s[i][j]);
int ans=0;
for(int i=1;i<=n;i++)
{
int tmp=count(i,n);
if(tmp>ans)
ans=tmp;
}
printf("%d\n",ans);
}
return 0;
}