Codeforces 429B Working out
http://codeforces.com/contest/429/problem/B
题意:一个从左下到右上,一个从左上到右下,要求只相交一次,求整个路径和的最大值
思路:发现可以枚举交点,然后算到四个角的值,而且,边界上面的点不可能作为交点。
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 int n,m,f1[1005][1005],f2[1005][1005],f4[1005][1005],f3[1005][1005]; 7 int a[1005][1005]; 8 int read(){ 9 int t=0,f=1;char ch=getchar(); 10 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 11 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 12 return t*f; 13 } 14 int main(){ 15 n=read();m=read(); 16 for (int i=1;i<=n;i++) 17 for (int j=1;j<=m;j++) 18 a[i][j]=read(); 19 for (int i=1;i<=n;i++) 20 for (int j=1;j<=m;j++) 21 f1[i][j]=std::max(f1[i-1][j],f1[i][j-1])+a[i][j]; 22 for (int i=1;i<=n;i++) 23 for (int j=m;j>=1;j--) 24 f2[i][j]=std::max(f2[i-1][j],f2[i][j+1])+a[i][j]; 25 for (int i=n;i>=1;i--) 26 for (int j=1;j<=m;j++) 27 f3[i][j]=std::max(f3[i+1][j],f3[i][j-1])+a[i][j]; 28 for (int i=n;i>=1;i--) 29 for (int j=m;j>=1;j--) 30 f4[i][j]=std::max(f4[i+1][j],f4[i][j+1])+a[i][j]; 31 int ans=0; 32 for (int i=2;i<n;i++) 33 for (int j=2;j<m;j++){ 34 ans=std::max(ans,f1[i-1][j]+f2[i][j+1]+f3[i][j-1]+f4[i+1][j]); 35 ans=std::max(ans,f1[i][j-1]+f2[i-1][j]+f3[i+1][j]+f4[i][j+1]); 36 } 37 printf("%d\n",ans); 38 return 0; 39 }