Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 2194 | Accepted: 1326 |
Description
The Mars Odyssey orbiter identified a rectangular area on the surface of Mars that is rich in minerals. The area is divided into cells that form a matrix of n rows and m columns, where the rows go from east to west and the columns go from north to south. The orbiter determined the amount of yeyenum and bloggium in each cell. The astronauts will build a yeyenum refinement factory west of the rectangular area and a bloggium factory to the north. Your task is to design the conveyor belt system that will allow them to mine the largest amount of minerals.
There are two types of conveyor belts: the first moves minerals from east to west, the second moves minerals from south to north. In each cell you can build either type of conveyor belt, but you cannot build both of them in the same cell. If two conveyor belts of the same type are next to each other, then they can be connected. For example, the bloggium mined at a cell can be transported to the bloggium refinement factory via a series of south-north conveyor belts.
The minerals are very unstable, thus they have to be brought to the factories on a straight path without any turns. This means that if there is a south-north conveyor belt in a cell, but the cell north of it contains an east-west conveyor belt, then any mineral transported on the south-north conveyor beltwill be lost. The minerals mined in a particular cell have to be put on a conveyor belt immediately, in the same cell (thus they cannot start the transportation in an adjacent cell). Furthermore, any bloggium transported to the yeyenum refinement factory will be lost, and vice versa.
Your program has to design a conveyor belt system that maximizes the total amount of minerals mined,i.e., the sum of the amount of yeyenum transported to the yeyenum refinery and the amount of bloggium transported to the bloggium refinery.
Input
The input is terminated by a block with n = m = 0.
Output
Sample Input
4 4
0 0 10 9
1 3 10 0
4 2 1 3
1 1 20 0
10 0 0 0
1 1 1 30
0 0 5 5
5 10 10 10
0 0
Sample Output
98
Hint
Source
容易的状态转换:
dp[i][j] = max(dp[i][j-1]+up[i][j],dp[i-1][j]+Left[i][j],dp[i-1][j-1]+Left[i][j-1]+up[i-1][j]+max(yey[i][j],blo[i][j]));
up[i][j] 还有left[i][j]可以预处理出来
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define N 510
using namespace std;
int yey[N][N],blo[N][N];
int up[N][N],Left[N][N],dp[N][N];
int main()
{
//freopen("data.in","r",stdin);
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
if(n==0&&m==0)
{
break;
}
memset(Left,0,sizeof(Left));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&yey[i][j]);
Left[i][j] = Left[i][j-1] + yey[i][j];
}
}
memset(up,0,sizeof(up));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&blo[i][j]);
up[i][j] = up[i-1][j] + blo[i][j];
}
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int k = max(dp[i][j-1]+up[i][j],dp[i-1][j]+Left[i][j]);
k = max(k,dp[i-1][j-1]+Left[i][j-1]+up[i-1][j]+max(yey[i][j],blo[i][j]));
dp[i][j] = max(k,dp[i][j]);
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}