首页 > 代码库 > nyist 18---- hdu 2048----poj 1163-----rwkj 1143

nyist 18---- hdu 2048----poj 1163-----rwkj 1143

 

//记忆式搜索
#include<iostream>
#include<string.h>
using namespace std;
int a[101][101],n,b[101][101];

int f(int i,int j)
{ if ( b[i][j]!=-1 ) return b[i][j];

if ( i==n-1) return b[i][j]=a[i][j];

else return b[i][j]=a[i][j]+max(f(i+1,j),f(i+1,j+1));
}


int main()
{
int i,j;
memset(b,-1,sizeof(b));
cin>>n;
int maxn=0;
for (i=0;i<n;i++)
for ( j=0;j<=i;j++)
cin>>a[i][j];
cout<<f(0,0)<<endl;
return 0;
}

 

 

 

 

 

 

 

#include <iostream>
#include<string.h>
using namespace std;
int a[1005][1005],b[1005][1005];
int n;
int max(int x, int y)
{ return x>y?x:y ; }

int f(int i,int j)
{
if(b[i][j]!=-1) return b[i][j];
return b[i][j]=a[i][j]+max(f(i+1,j),f(i+1,j+1));
}

int main()
{
int i,j;

while(cin>>n)
{

for(i=0;i<n;i++)
for(j=0;j<=i;j++) cin>>a[i][j];
memset(b,-1,sizeof(b));
for(j=0;j<n;j++) b[i-1][j]=a[i-1][j];
cout<<f(0,0)<<endl;
}

}