[luogu1004]方格取数(dp,亚瑟)

某人从图中的左上角的A出发,可以向下行走,也可以向右行走,直到达右下角的B点。在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字0)。
此人从A点到B点共走了两次,试找出两条这样的路径,使得取得的数字和为最大。

INPUT
8
2 3 13
2 6 6
3 5 7
4 4 14
5 2 21
5 6 4
6 3 15
7 2 14
0 0 0

OUTPUT
67

#include<iostream>
using namespace std;
const int maxn=101;

int a[maxn][maxn],f[maxn*2][maxn][maxn];
int n,m;

int main(){
    cin>>n;
    int x=1,y,w;
    while (1){
        cin>>x>>y>>w;
        a[x][y]=w;
        if (x==0&&y==0&&w==0) break;
    }
    for (int l=2;l<=2*n;l++) //阶段
        for (int x1=1;x1<=n;x1++)
            for (int x2=1;x2<=n;x2++){
                        int y1=l-x1; 
                        int y2=l-x2;
                        if (y1<=0) continue;
                        if (y2<=0) continue;    
                        int s=f[l][x1][x2];     
                        s=max(s,f[l-1][x1-1][x2-1]);
                        s=max(s,f[l-1][x1][x2-1]);
                        s=max(s,f[l-1][x1-1][x2]);
                        s=max(s,f[l-1][x1][x2]);
                        f[l][x1][x2]+=s;
                        if (x1==x2&&y1==y2) f[l][x1][x2]+=a[x1][y1];   //重合
                        else f[l][x1][x2]+=a[x1][y1]+a[x2][y2];
            }
    cout<<f[2*n][n][n];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值