poj 2948 Martian Mining 夜

http://poj.org/problem?id=2948

矩阵中每个点都有 只能向north和向west传递的两种矿 二者不可兼得

传到最north 和最 west 的矿才可获得  求最大

DP 递推

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

const int N=505;
int tonorth[N][N];
int towest[N][N];
int sum[N][N];
int main()
{
   int n,m;
   while(scanf("%d %d",&n,&m)!=EOF)
   {
       if(n==0&&m==0)
       break;
       memset(towest,0,sizeof(towest));
       memset(tonorth,0,sizeof(tonorth));
       for(int i=1;i<=n;++i)
       for(int j=1;j<=m;++j)
       scanf("%d",&towest[i][j]);
       for(int i=1;i<=n;++i)
       for(int j=1;j<=m;++j)
       scanf("%d",&tonorth[i][j]);
       for(int i=n;i>=1;--i)
       {
           for(int j=m;j>=1;--j)
           {
               towest[i][j]+=towest[i+1][j];
               tonorth[i][j]+=tonorth[i][j+1];
           }
       }
       for(int i=n;i>=1;--i)
       {
           for(int j=m;j>=1;--j)
           {
               sum[i][j]=max(sum[i+1][j]+tonorth[i][j],sum[i][j+1]+towest[i][j]);
           }
       }
       printf("%d\n",sum[1][1]);
   }

   return 0;
}

  

原文地址:https://www.cnblogs.com/liulangye/p/2598845.html