HDU-2686 Matrix 多进程DP

时间:2024-04-18 12:13:33

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686

  经典的多进程DP,比较简单。f[x1][y1][x2][y2]表示起点到点(x1,y1)和(x2,y2)的最优值,然后分层转移就可以了,每一层为斜向右的线。。

 //STATUS:C++_AC_46MS_6172KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int d[][]={{-,,-,},{-,,,-},{,-,,-},{,-,-,}};
int ma[N][N],f[N][N][N][N],xy[*N][];
int n; int main(){
// freopen("in.txt","r",stdin);
int i,j,q,p,k,x0,y0,w,x1,y1,x2,y2,nx1,ny1,nx2,ny2,up;
while(~scanf("%d",&n))
{
for(i=;i<n;i++){
for(j=;j<n;j++)
scanf("%d",&ma[i][j]);
}
mem(f,);
f[][][][]=ma[][]+ma[][]+ma[][];
up=(n-)<<|;x0=,y0=;w=;
for(i=;i<up-;i++){
i<=up/?(x0++,w++):(y0++,w--);
for(j=;j<w;j++){
xy[j][]=x0-j;
xy[j][]=y0+j;
}
for(q=;q<w;q++){
for(p=q+;p<w;p++){
x1=xy[q][],y1=xy[q][];
x2=xy[p][],y2=xy[p][];
for(k=;k<;k++){
nx1=x1+d[k][],ny1=y1+d[k][];
nx2=x2+d[k][],ny2=y2+d[k][];
if(nx1==nx2 && ny1==ny2)continue;
if(nx1<||ny1< || nx2<||ny2<)continue;
f[x1][y1][x2][y2]=Max(f[x1][y1][x2][y2],f[nx1][ny1][nx2][ny2]+ma[x1][y1]+ma[x2][y2]);
}
}
}
} n--;
printf("%d\n",f[n][n-][n-][n]+ma[n][n]); }
return ;
}