递推DP UVA 1366 Martian Mining

时间:2023-03-08 16:25:05

题目传送门

 /*
题意:抽象一点就是给两个矩阵,重叠的(就是两者选择其一),两种铺路:从右到左和从下到上,中途不能转弯,
到达边界后把沿途路上的权值相加求和使最大
DP:这是道递推题,首先我题目看了老半天,看懂后写出前缀和又不知道该如何定义状态好,写不出状态转移方程,太弱了。
dp[i][j]表示以(i, j)为右下角时求得的最大值,状态转移方程:dp[i][j] = max (dp[i-1][j] + sum1[i][j], dp[i][j-1] + sum2[i][j]); sum1表示列的前缀,sum2表示行的前缀
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-9 10:18:37
* File Name :UVA_1366.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int a[MAXN][MAXN], b[MAXN][MAXN];
int sum1[MAXN][MAXN], sum2[MAXN][MAXN];
int dp[MAXN][MAXN]; int main(void) { //UVA 1366 Martian Mining
int n, m;
while (scanf ("%d%d", &n, &m) == ) {
if (!n && !m) break;
memset (sum1, , sizeof (sum1));
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
scanf ("%d", &a[i][j]); sum1[i][j] = sum1[i][j-] + a[i][j];
}
}
memset (sum2, , sizeof (sum2));
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
scanf ("%d", &b[i][j]); sum2[i][j] = sum2[i-][j] + b[i][j];
}
}
memset (dp, , sizeof (dp));
for (int i=; i<=n; ++i) {
for (int j=; j<=m; ++j) {
dp[i][j] = max (dp[i-][j] + sum1[i][j], dp[i][j-] + sum2[i][j]);
}
}
printf ("%d\n", dp[n][m]);
} return ;
}