题意:
在一个n∗m(n,m<=500)的地图里有R矿和U矿,当你在某个点(x,y),可以建造一个只能向西或者向北挖取矿石的履带车
R矿石只能被向北移动的履带车挖取,U矿石只能被向西移动的履带车挖取,并且履带车不能交叉
$给出你每个地点的R矿和U矿的数量, 求最大运送的矿石数目(履带车会一直走到边界)
分析:
当你位于(x, y)时, 你只有两个操作:
1)建立一个向北运输的履带车, 挖一路上的R矿石
2)建立一个想西运输的履带车, 挖一路上的U矿石
考虑dp[i][j]:=(1,1)→(i,j)这个矩形区域所能运送的最大矿石数量
预处理出(i,j)这点向北运送的R矿石的前缀和,向西的U矿石的前缀和
转移对于(i,j),向北dp[i][j−1]+sumR(i,j),向西dp[i−1][j]+sumU(i,j)
dp[i][j]=max(dp[i][j−1]+sumR(i,j),dp[i−1][j]+sumU(i,j))
ans=dp[n][m]
代码:
//
// Created by TaoSama on 2015-11-13
// Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m, a[505][505], b[505][505], dp[505][505];
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
int kase = 0;
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) { //U to west
for(int j = 1; j <= m; ++j) {
int x; scanf("%d", &x);
a[i][j] = a[i][j - 1] + x;
}
}
for(int i = 1; i <= n; ++i) { //R to north
for(int j = 1; j <= m; ++j) {
int x; scanf("%d", &x);
b[i][j] = b[i - 1][j] + x;
}
}
memset(dp, 0, sizeof dp); //(1,1)~(i,j) rectangle
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]);
printf("Case %d: %d\n", ++kase, dp[n][m]);
}
return 0;
}