poj2948--Martian Mining(dp)

Martian Mining

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 2459   Accepted: 1517

Description

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.

Input

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.

Output

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

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

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

给出每一个格子的矿的数量,只能向左或向右,并且道路只能是直线,所以每次只能加一行或一列

dp[i][j]代表从(1,1)到(i,j)可以得到的最大和,Map1向左Map1[i][j]第i行从第1个到第j个的和,Map2[i][j]代表第j列从第1个到第i个的和。

dp[i][j] = max( dp[i-1][j]+Map1[i][j],dp[i][j-1]+Map2[i][j] )

#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std ;

int Map1[510][510] , Map2[510][510];

int dp[510][510] ;

int n , m ;

int main()

{

int i , j , ans ;

while( scanf("%d %d", &n, &m) != EOF )

{

if( n == 0 && m == 0 ) break ;

ans = 0 ;

memset(dp,0,sizeof(dp)) ;

memset(Map1,0,sizeof(Map1)) ;

memset(Map2,0,sizeof(Map2)) ;

for(i = 1 ; i <= n ; i++)

for(j = 1 ; j <= m ; j++)

{

scanf("%d", &Map1[i][j]) ;

Map1[i][j] += Map1[i][j-1] ;

}

for(i = 1 ; i <= n ; i++)

for(j = 1 ; j <= m ; j++)

{

scanf("%d", &Map2[i][j]) ;

Map2[i][j] += Map2[i-1][j] ;

}

for(i = 1 ; i <= n ; i++)

{

for(j = 1 ; j <= m ; j++)

dp[i][j] = max( dp[i-1][j]+Map1[i][j],dp[i][j-1]+Map2[i][j] ) ;

}

printf("%d\n", dp[n][m] ) ;

}

return 0;

}

时间: 2024-10-10 20:42:31

poj2948--Martian Mining(dp)的相关文章

poj 2948 Martian Mining (dp)

题目链接 完全自己想的,做了3个小时,刚开始一点思路没有,硬想了这么长时间,想了一个思路, 又修改了一下,提交本来没抱多大希望 居然1A了,感觉好激动..很高兴dp又有所长进. 题意: 一个row*col的矩阵,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个 格子内的数量是多少.最北边有bloggium的收集站,最西边有 yeyenum 的收集站. 现在要在这些格子上面安装向北或者向西的传送带(每个格子自能装一种).问最多能采到多少矿. 传送带只能直着走,不可弯曲,不能交

POJ 2948 Martian Mining(DP)

题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿. 思路 :记忆化搜索.也可以用递推. //2948 #include <stdio.h> #include <string.h> #include <iostream> using namespace std ; int yeye[510][510] ,blog[510]

递推DP UVA 1366 Martian Mining

题目传送门 1 /* 2 题意:抽象一点就是给两个矩阵,重叠的(就是两者选择其一),两种铺路:从右到左和从下到上,中途不能转弯, 3 到达边界后把沿途路上的权值相加求和使最大 4 DP:这是道递推题,首先我题目看了老半天,看懂后写出前缀和又不知道该如何定义状态好,写不出状态转移方程,太弱了. 5 dp[i][j]表示以(i, j)为右下角时求得的最大值,状态转移方程:dp[i][j] = max (dp[i-1][j] + sum1[i][j], dp[i][j-1] + sum2[i][j])

POJ 2948 Martian Mining

题目大意: NASA在火星发现了一个矿场矩阵.矩阵中的每个单元格都有两种矿Yeyenum和Bloggium.我们知道每个单元格中这两种矿的数量.NASA决定在北边建造Bloggium的矿石精炼厂,在西边建造Yeyenum的矿石精炼厂.于是需要我们把bloggium矿石向北运(行号等于0的方向),把Yeyenum矿石向西运(列号等于0的方向).但由于矿石的不稳定在建造传送带时有特殊要求.求建造传送带后两种矿石最多能收集多少. 解题思路: dp[i][j]代表着从(0,0)到(i,j)这两点间组成的

UVA 1366 九 Martian Mining

Martian Mining Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1366 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 int dp[505][505][3]; 7

UVa1366Martian Mining (DP)

Martian Mining Description 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 spac

UVA - 1366 Martian Mining 三维dp

题目大意:在一个n * m的格子上有两种矿物.A矿只能从右往左运输,B矿只能从下往上运输,现在要求你在这格子上铺设管道(管道不能中断或者拐弯),用管道来运输A矿和B矿,使得运输的A矿和B矿的和最大 解题思路:设dp[i][j][0]为以(i,j)为矩阵的右下角,铺设从右往左的管道所能运输的最大值,那么第i行的第1列到第j列只能铺设从右往左的管道,所以dp[i][j][0] = max(dp[i-1][j][0],dp[i-1][j][1]) + A[i][j] 设dp[i][j][1]为以(i,

(中等) POJ 2948 Martian Mining,DP。

Description 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.

UVa 1366 DP Martian Mining

网上的题解几乎都是一样的: d(i, j, 0)表示前i行前j列,第(i, j)个格子向左运输能得到的最大值. d(i, j, 1)是第(i, j)个格子向上运输能得到的最大值. 但是有一个很关键的问题没有解释: 某个格子中的A矿或者B矿一定有其中一种能够运出来吗?有没有可能这个格子没有修建管道,仅仅是为了给其他管道让路,使得总体取得最大值呢? 从题解的状态转移方程上来看,不会的. 自己YY了好久,也没有想到一个能严格证明的方法.. 1 #include <iostream> 2 #inclu