POJ1157 LITTLE SHOP OF FLOWERS

挺简单的DP。DP[x][y]表示的是到了第x束花插到y花瓶的最大价值,则可知x束花只有插到y花瓶和不插到y花瓶两种状态,由此可知状态转移方程为

dp[x][y]=max(dp[x][y-1],dp[x-1][y-1]+value[x][y])

以下就是全部代码,不优化了

/*******************************************************************************
 # Author : Neo Fung
 # Email : [email protected]
 # Last modified: 2012-03-16 20:11
 # Filename: POJ1157 LITTLE SHOP OF FLOWERS.cpp
 # Description : 
 ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
#define MAX 110
using namespace std;

int dp[MAX][MAX];
int value[MAX][MAX];

int main(void)
{
#ifdef DEBUG  
  freopen("../stdin.txt","r",stdin);
  freopen("../stdout.txt","w",stdout); 
#endif  

	int n,m;

	while(~scanf("%d%d",&n,&m))
	{
    memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j)
			{
				scanf("%d",&value[i][j]);
				dp[i][j]=INT_MIN;
			}

		for(int i=1;i<=n;++i)
		{
			dp[i][i]=dp[i-1][i-1]+value[i][i];
			for(int j=i+1;j<=m;++j)
				dp[i][j]=max(dp[i][j-1],dp[i-1][j-1]+value[i][j]);
		}
		printf("%d\n",dp[n][m]);
	}

  return 0;
}


你可能感兴趣的:(POJ1157 LITTLE SHOP OF FLOWERS)