[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];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章