1. 程式人生 > >3530 Martian Mining

3530 Martian Mining

題意:每個格子放左箭頭或者上箭頭,要求箭頭不能中斷,順著箭頭走的時候不能轉折(比如一個左箭頭左面有一個上箭頭或者一個上箭頭上面有一個左箭頭,但是允許上箭頭下面有左箭頭,允許左箭頭右面有上箭頭),左箭頭獲得的價值為a[i][j],上箭頭獲得的價值為b[i][j],問把圖佈滿箭頭的最大價值為多少

字首和和dp搞一搞就可以了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int n, m, a[maxn][maxn], b[maxn][maxn], dp[maxn][maxn], suma[maxn][maxn], sumb[maxn][maxn];
int main() {
    while (~scanf("%d%d", &n, &m)&&n+m) {
        memset(suma, 0, sizeof(suma));
        memset(sumb, 0, sizeof(sumb));
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]), suma[i][j] = suma[i][j-1]+a[i][j];
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) scanf("%d", &b[i][j]), sumb[i][j] = sumb[i-1][j]+b[i][j];
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = max(dp[i-1][j]+suma[i][j], dp[i][j-1]+sumb[i][j]);
            }
        }
        printf("%d\n", dp[n][m]);
    }
    return 0;
}