Codeforces 429B Working out

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e3+5;
 4 const int INF=1e9+7;
 5 int n,m,ans,a[maxn][maxn],dp1[maxn][maxn],dp2[maxn][maxn],dp3[maxn][maxn],dp4[maxn][maxn];
 6 template <class t>void red(t &x)
 7 {
 8     x=0;
 9     int w=1;
10     char ch=getchar();
11     while(ch<'0'||ch>'9')
12     {
13         if(ch=='-')
14             w=-1;
15         ch=getchar();
16     }
17     while(ch>='0'&&ch<='9')
18     {
19         x=(x<<3)+(x<<1)+ch-'0';
20         ch=getchar();
21     }
22     x*=w;
23 }
24 void input()
25 {
26     freopen("input.txt","r",stdin);
27 }
28 void read()
29 {
30     red(n);
31     red(m);
32     for(int i=1;i<=n;++i)
33         for(int j=1;j<=m;++j)
34             red(a[i][j]);    
35 }
36 void work()
37 {
38     for(int i=1;i<=n;++i)
39         for(int j=1;j<=m;++j)    
40             dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1])+a[i][j];
41     for(int i=1;i<=n;++i)
42         for(int j=m;j>=1;--j)
43             dp2[i][j]=max(dp2[i-1][j],dp2[i][j+1])+a[i][j];
44     for(int i=n;i>=1;--i)
45         for(int j=1;j<=m;++j)
46             dp3[i][j]=max(dp3[i+1][j],dp3[i][j-1])+a[i][j];
47     for(int i=n;i>=1;--i)
48         for(int j=m;j>=1;--j)
49             dp4[i][j]=max(dp4[i+1][j],dp4[i][j+1])+a[i][j];
50     for(int i=2;i<n;++i)
51         for(int j=2;j<m;++j)
52             ans=max(max(dp1[i-1][j]+dp3[i][j-1]+dp2[i][j+1]+dp4[i+1][j],dp2[i-1][j]+dp1[i][j-1]+dp4[i][j+1]+dp3[i+1][j]),ans);
53     printf("%d",ans);
54 }
55 int main()
56 {
57     //input();
58     read();
59     work();
60     return 0;
61 }
View Code

题面

原文地址:https://www.cnblogs.com/Achensy/p/10775821.html