TOJ 1800 Martian Mining(二维dp)

Posted taozi1115402474

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOJ 1800 Martian Mining(二维dp)相关的知识,希望对你有一定的参考价值。

描述

The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM Finals this year). This is the place where the astronauts are trained for Mission Seven Dwarfs, the next giant leap in space exploration. The Mars Odyssey program revealed that the surface of Mars is very rich in yeyenum and bloggium. These minerals are important ingredients for certain revolutionary new medicines, but they are extremely rare on Earth. The aim of Mission Seven Dwarfs is to mine these minerals on Mars and bring them back to Earth.

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.

输入

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 500 of rows, and the number 1 ≤ m ≤ 500 of columns. The next n lines describe the amount of yeyenum that can be found in the cells. Each of these n lines contains m integers. The first line corresponds to the northernmost row; the first integer of each line corresponds to the westernmost cell of the row. The integers are between 0 and 1000. The next n lines describe in a similar fashion theamount of bloggium found in the cells.

The input is terminated by a block with n = m = 0.

输出

For each test case, you have to output a single integer on a separate line: the maximum amount of mineralsthat can be mined.

样例输入

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

样例输出

98

提示

Huge input file, ‘scanf‘ recommended to avoid TLE.

题意

N*M的方格,每个方格里有A和B矿石,A矿石只能往左运输,B只能往上,每个方格只能放最多1个传送带,问你最大能得到多少矿石

题解

dp[i][j]代表从(1,1)->(n,m)所得到的最大矿石

可以知道dp[i][j]=max(dp[i-1][j]+a[i][j],dp[i][j-1]+b[i][j]);

dp[i-1][j]+a[i][j];代表(1,1)->(i-1,j)的最大值,加上当前点左边的全部左移

dp[i][j-1]+a[i][j];代表(1,1)->(i,j-1)的最大值,加上当前点上边的全部上移

初始值i=0或j=0,dp[i][j]=0

代码

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N=505;
 6 int n,m;
 7 int dp[N][N],a[2][N][N];
 8 int main()
 9 {
10     while(scanf("%d%d",&n,&m)!=EOF,n||m)
11     {
12         for(int k=0;k<2;k++)
13             for(int i=1;i<=n;i++)
14                 for(int j=1;j<=m;j++)
15                 {
16                     scanf("%d",&a[k][i][j]);
17                     if(!k)a[0][i][j]+=a[0][i][j-1];
18                     else a[1][i][j]+=a[1][i-1][j];
19                 }
20         memset(dp,0,sizeof dp);
21         for(int i=1;i<=n;i++)
22             for(int j=1;j<=m;j++)
23                 dp[i][j]=max(dp[i-1][j]+a[0][i][j],dp[i][j-1]+a[1][i][j]);
24         printf("%d
",dp[n][m]);
25     }
26     return 0;
27 }

 




















以上是关于TOJ 1800 Martian Mining(二维dp)的主要内容,如果未能解决你的问题,请参考以下文章

Martian Mining_DP&&DFS

[poj 2948]Martian Mining

poj 2948 martian mining

TOJ 5439数据结构实验:平衡二叉树

TOJ 1224数据结构练习题――后序遍历二叉树

TOJ 1003