| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
16ms 0ms的晚上无人的时候再提交就可以了。,。。呵呵。。ac了,谢谢洵,让我更深的了解DP,贴代码留念一下,。。。#include<cstdio>
#include<iostream>
using namespace std;
#define N 1000
#define MAX(a,b) (a)>(b)?(a):(b)
int a[N][N];
int dp[N][N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 1;i<=n;i++)
for(int j = 1;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i = 1;i<=n;i++)
for(int j = 0;j<=m;j++)
dp[i][j] = -1<<29;
for(int j = 0;j<=m;j++)//初始化最外层为0
dp[0][j] = 0;
for(int i = 1;i<=n;i++)
for(int j = i;j<=m;j++)
dp[i][j] = MAX(dp[i][j-1],MAX(dp[i][j],dp[i-1][j-1] + a[i][j]));//跟前面与上面的最优值比较
printf("%d\n",dp[n][m]);//最后的那个肯定是所有的值中最大的
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator