1. 程式人生 > >UVALive - 3530:Martian Mining

UVALive - 3530:Martian Mining

main ace pre art nbsp break pan void lib

dp

可以發現,對於(i,j),要麽把它運上去,那麽把它運到左邊,枚舉一下即可

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 505
using namespace std;
int f[MAXN][MAXN];
int a[MAXN][MAXN],b[MAXN][MAXN];
int s[MAXN][MAXN],t[MAXN][MAXN];
int n,m;
void solve(){
    for(int i=1;i<=n;i++){
        
for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); s[i][j]=s[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]); t[i][j]=t[i-1][j]+b[i][j]; } } for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){ f[i][j]=max(f[i-1][j]+s[i][j],f[i][j-1]+t[i][j]); } } printf("%d\n",f[n][m]); } int main() { while(1){ scanf("%d%d",&n,&m); if(!n) break; solve(); } return 0; }

UVALive - 3530:Martian Mining