题意:题意:一个row*col的矩阵,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个格子内的数量是多少。如图所示,最北边有bloggium的收集站,最西边有 yeyenum 的收集站。现在要你在这些格子上面安装向北或者向西的传送带(每个格子自能装一种)。问最多能采到多少矿(yeyenum+bloggium)?
题解:注意一下每个格子只能向西或者向北,某个格子假如被选定向西,那么它所在的这一行全都向西。
#include <iostream>
using namespace std;
#define N 502
#define max(a,b) ( a > b ? a : b )
int dp[N][N], north[N][N], west[N][N];
int main()
{
int row, col, r, c, ye, bl;
while ( scanf("%d%d",&row,&col) && (row+col) )
{
memset(west,0,sizeof(west));
memset(north,0,sizeof(north));
memset(dp,0,sizeof(dp));
for ( r = 1; r <= row; r++ )
{
for ( c = 1; c <= col; c++ )
{
scanf("%d",&ye);
west[r][c] = west[r][c-1] + ye;
}
}
for ( r = 1; r <= row; r++ )
{
for ( c = 1; c <= col; c++ )
{
scanf("%d",&bl);
north[r][c] = north[r-1][c] + bl;
}
}
for ( r = 1; r <= row; r++ )
{
for ( c = 1; c <= col; c++ )
dp[r][c] = max ( west[r][c] + dp[r-1][c], north[r][c] + dp[r][c-1] );
}
printf("%d\n",dp[row][col]);
}
return 0;
}