Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

人生中的第一个DP题,mark

Posted by xiexinxinlove at 2014-07-22 16:50:04 on Problem 1163
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = 110;

int d[Max][Max];
int a[Max][Max];
int n;

int maxNum(int a, int b)
{
	if(a > b)
		return a;
	else
	 	return b;
}

int solve(int i, int j) //求解d[i][j]的值,即从a[i][j]到底端能得到的最大的数 
{                       //记忆化搜索,已经搜索过的,放进d数组中 
	if(d[i][j] > 0) //说明已经有值了,直接返回 
	{
		return d[i][j];
	}
	else if(i == n)
	{
		return d[i][j] = a[i][j]; //如果是底层,直接赋给a[i][j] 
	}
	else 
	{
		return d[i][j] = a[i][j] + maxNum(solve(i+1,j),solve(i+1,j+1));
	}
}
int main()
{
	int i,j;
	while(scanf("%d",&n) != EOF)
	{
		memset(d,-1,sizeof(d));
		for(i=0; i<n; i++)
		{
			for(j=0; j<=i; j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		printf("%d\n",solve(0,0));
	}
	return 0;
}
	

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator