UVALive - 3530 Martian Mining

题意:给出n*m网格中每个格子有A,B两种矿,A矿必须从右向左运输,B矿必须从下往上运输,管子不能拐弯或者折断,要求收集到的A,B矿的总量尽量大

思路:对于每个格子不是安A就是B矿的管道,所以只有两个选择。

所以dp[i][j]=max(dp[i][j-1]+b[i][j],dp[i-1][j]+a[i][j]),其中a[i][j],b[i][j]分别代表从右往左和从下往上运输方式的前j,前i的总矿数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 505;

int a[MAXN][MAXN],b[MAXN][MAXN],dp[MAXN][MAXN];
int n,m;

int main(){
    while (scanf("%d%d",&n,&m) != EOF && n+m){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++){
                scanf("%d",&a[i][j]);
                a[i][j] += a[i][j-1];
            }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++){
                scanf("%d",&b[i][j]);
                b[i][j] += b[i-1][j];
            }
        memset(dp,0,sizeof(dp));
        int ans = 0;
        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("%d\n",dp[n][m]);
    }
    return 0;
}



UVALive - 3530 Martian Mining

上一篇:unity3d笔记(2)——基本操作(一)


下一篇:Java序列化(一)