codefore 213 C Relay Race (朴素DP)
原题地址:http://codeforces.com/problemset/problem/213/C
题意:略
题解:dp,状态为点的位置,直接枚举复杂度是n4,利用两个点坐标之和之间的关系,优化到n3。
#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int maxn=300; const int INF=1e8; int A[maxn+5][maxn+5]; int dp[maxn+5][maxn+5]; int dx[]={-1,-1,0,0}; int dx2[]={-1,0,-1,0}; int n; int main(void) { #ifdef ex freopen ("../in.txt","r",stdin); //freopen ("../out.txt","w",stdout); #endif scanf("%d",&n); for (int i=1;i<=n;++i) { for (int j=1;j<=n;++j) { scanf("%d",&A[i][j]); } } int x1,y1,x2,y2,tmp; //cout<<dp[1][1][1]<<' '<<dp[2][1][1]<<endl; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) dp[i][j]=-INF; dp[1][1]=A[1][1]; for (int i=3;i<=2*n;++i) { for (int j=min(i-1,n);j>=1;--j) { for (int k=min(i-1,n);k>=1;--k) { int T=-INF; for (int q=0;q<=3;++q) { x1=j+dx[q]; x2=k+dx2[q]; y1=i-1-x1; y2=i-1-x2; if (x1<=0 || x1>n || y1<=0 || y1>n) continue; if (x2<=0 || x2>n || y2<=0 || y2>n) continue; if (j==k) tmp=A[j][i-j]; else tmp=A[j][i-j]+A[k][i-k]; T=max(T,dp[x1][x2]+tmp); } dp[j][k]=T; //printf("%d %d %d %d\n",i,j,k,dp[j][k]); } } } int ans=dp[n][n]; printf("%d\n",ans); }
posted on 2016-08-22 19:57 zhong_wang 阅读(256) 评论(0) 编辑 收藏 举报