首页 > 代码库 > zzuli 1875多线DP
zzuli 1875多线DP
1875: 蛤玮的财宝
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 530 Solved: 116
SubmitStatusWeb Board
Description
Input
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
#define CIN(a) scanf("%d",&a)
#define ql(a) memset(a,0,sizeof(a))
int dp[210][105][105];
int e[105][105];
int main()
{
int t,i,j,n,m;
int x1,x2,k;
CIN(t);
while(t--){ql(e),ql(dp);
CIN(n),CIN(m);
for(i=1;i<=n;++i)
for(j=1;j<=m;++j) CIN(e[i][j]);
for(k=1;k<n+m-2;++k)
for(x1=1;x1<=n;++x1)
for(x2=1;x2<=n;++x2){
int y1=k+2-x1,y2=k+2-x2;
if(x1==x2||y1<0||y2<0||y1>m||y2>m) continue;
dp[k][x1][x2]=max(max(dp[k-1][x1][x2],dp[k-1][x1][x2-1]),max(dp[k-1][x1-1][x2],dp[k-1][x1-1][x2-1]))+e[x1][y1]+e[x2][y2];
}
cout<<max(dp[k-1][n-1][n],dp[k-1][n][n-1])+e[n][m]+e[1][1]<<endl;
}
return 0;
}
注意处理起点如果没有初始化dp[0][x1][x2]的话最后切记加上e[1][1].
也可以选择先处理dp[0]不过不划算啦
zzuli 1875多线DP