首页 > 其他 > 详细

UVA 1366 - Martian Mining(dp)

时间:2014-02-21 03:16:21      阅读:306      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣

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 1bubuko.com,布布扣nbubuko.com,布布扣500 of rows, and the number 1bubuko.com,布布扣mbubuko.com,布布扣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

题意:如图中图所示,n*m位置上有A,B两种矿,A只能从左向右,B只能从下往上运输,然后要运输,运输过程不能有转弯,问最多的运的矿数。

思路:dp,dp[i][j]表示从左上角到(i,j)这个范围的矩阵最大运矿数,这样就看最下面这行是要往左还是往上运输,dp[i][j] = min(dp[i- 1][j] + suma[i][j], dp[i][j - 1] + sumb[i][j]);

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
const int N = 505;

int n, m, a[N][N], b[N][N], dp[N][N], i, j;

int solve() {
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			dp[i][j] = max(dp[i - 1][j] + a[i][j], dp[i][j - 1] + b[i][j]);
		}
	return dp[n][m];
}

int main() {
	while (~scanf("%d%d", &n, &m) && n + m) {
		for (i = 1; i <= n; i++)
			for (j = 1; j <= m; j++) {
				scanf("%d", &a[i][j]);
				a[i][j] += a[i][j - 1];
			}
		for (i = 1; i <= n; i++)
			for (j = 1; j <= m; j++) {
				scanf("%d", &b[i][j]);
				b[i][j] += b[i - 1][j];
			}
		printf("%d\n", solve());
	}
	return 0;
}


UVA 1366 - Martian Mining(dp)

原文:http://blog.csdn.net/accelerator_/article/details/19548339

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!