Acwing 0x50 动态规划~传纸条(NOIP2008复赛提高组第三题)

题目链接https://www.acwing.com/problem/content/277/

NOIP2008复赛提高组第三题,非常好的一个题,让我学会了很多东西

题意

给定一个 N*M 的矩阵A,每个格子中有一个整数。

现在需要找到两条从左上角 (1,1)到右下角(N,M) 的路径,路径上的每一步只能向右或向下走。

路径经过的格子中的数会被取走,两条路径可以经过同一个格子,但格子中的数 只能被取一次

求取得的数之和最大是多少。

输入格式

第一行有2个用空格隔开的整数nm,表示矩阵有nm列。

接下来的n行是一个n*m的矩阵,每行的m个整数之间用空格隔开。

输出格式

输出一个整数,表示答案。

数据范围

1\leq n,m\leq 50

题解:

设路径长度为i,第一条路径末尾位于(x_{1},y_{1}),第二条路径末尾位于(x_{2},y_{2})

容易发现有等式成立:x_{1}+y_{1}=x_{2}+y_{2}=i+21\leq x_{1},x_{2}\leq min(i+2)

f[i][x_{1}][x_{2}]表示两条路径长度为i,第一条路径末尾在第x_{1}行,第二条路径末尾在x_{2}行时,已经取得的最大值。

简单附代码:

#include <bits/stdc++.h>
#define Pair pair<int,int>
#define fir first
#define sec second
namespace fastIO{
  #define BUF_SIZE 100000
  #define OUT_SIZE 100000
  //fread->read
  bool IOerror=0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
  inline char nc(){
    static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
      if(p1==pend){
        p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);
        if(pend==p1){IOerror=1;return -1;}
      }
      return *p1++;
  }
  inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
  template<class T> inline bool read(T &x){
    bool sign=0;char ch=nc();x=0;
    for(;blank(ch);ch=nc());
    if(IOerror)return false;
    if(ch=='-')sign=1,ch=nc();
    for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
    if(sign)x=-x;
    return true;
  }
  inline bool read(double &x){
    bool sign=0;char ch=nc();x=0;
    for(;blank(ch);ch=nc());
    if(IOerror)return false;
    if(ch=='-')sign=1,ch=nc();
    for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
    if(ch=='.'){double tmp=1; ch=nc();for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');}
      if(sign)x=-x;return true;
  }
  inline bool read(char *s){
    char ch=nc();
    for(;blank(ch);ch=nc());
    if(IOerror)return false;
    for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
    *s=0;
    return true;
  }
  inline bool read(char &c){
    for(c=nc();blank(c);c=nc());
    if(IOerror){c=-1;return false;}
    return true;
  }
  template<class T,class... U>bool read(T& h,U&... t){return read(h)&&read(t...);}
  #undef OUT_SIZE
  #undef BUF_SIZE
};using namespace fastIO;using namespace std;

const int N=50+5;
const double eps=1e-7;
const double pi=acos(-1.0);
const int mod=998244353;
const int inf=0x3f3f3f3f;

int f[N*2][N][N],c[N][N];
int n,m;
signed main(){
  read(n,m);
  for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
      read(c[i][j]);
  
  f[0][1][1]=c[1][1];
  /*1 1 2 x+y=i+2*/
  for(int i=0;i<n+m-2;i++)
    for(int x1=1;x1<=n&&x1<=i+1;x1++)
      for(int x2=1;x2<=n&&x2<=i+1;x2++){
        int y1=i+2-x1,y2=i+2-x2;
        if(x1==x2){
          f[i+1][x1][x2]=max(f[i+1][x1][x2],f[i][x1][x2]+c[x1][y1+1]);//yy
          f[i+1][x1+1][x2+1]=max(f[i+1][x1+1][x2+1],f[i][x1][x2]+c[x1+1][y1]);//xx
        }
        else{
          f[i+1][x1][x2]=max(f[i+1][x1][x2],f[i][x1][x2]+c[x1][y1+1]+c[x2][y2+1]);//yy
          f[i+1][x1+1][x2+1]=max(f[i+1][x1+1][x2+1],f[i][x1][x2]+c[x1+1][y1]+c[x2+1][y2]);//xx
        }
        //x y
        if(x1+1==x2) f[i+1][x1+1][x2]=max(f[i+1][x1+1][x2],f[i][x1][x2]+c[x1+1][y1]);//xy
        else f[i+1][x1+1][x2]=max(f[i+1][x1+1][x2],f[i][x1][x2]+c[x1+1][y1]+c[x2][y2+1]);//xy 
        
        if(x2+1==x1) f[i+1][x1][x2+1]=max(f[i+1][x1][x2+1],f[i][x1][x2]+c[x2+1][y2]);//yx          
        else f[i+1][x1][x2+1]=max(f[i+1][x1][x2+1],f[i][x1][x2]+c[x1][y1+1]+c[x2+1][y2]);//yx 
      }
  printf("%lld\n",f[n+m-2][n][n]);

  return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值