题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708
测试数据:
5
1 9 9 1 1
1 9 9 9 9
9 9 9 9 9
9 9 9 9 1
1 1 9 9 1
81 1
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 using namespace std; 5 long long a[15][15]; 6 long long step,ans; 7 void solve1(int t,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) 8 { 9 int i; 10 long long w; 11 t = t-1;//范围想错了,wa到比赛结束还不对,唉 12 for(i=0;i<t;i++) 13 { 14 w = a[x1][y1] + a[x2][y2]+a[x3][y3]+a[x4][y4]; 15 y1++; 16 x2++; 17 y3--; 18 x4--; 19 if(w>ans) 20 { 21 ans = w; 22 step = i; 23 } 24 } 25 } 26 void solve2(int t,int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4) 27 { 28 int i; 29 t =t-1; 30 long long w; 31 for(i=0;i<t;i++) 32 { 33 w = a[x1][y1] + a[x2][y2]+a[x3][y3]+a[x4][y4]; 34 x1++; 35 y2--; 36 x3--; 37 y4++; 38 if(w>=ans) 39 { 40 ans = w; 41 if(step>i) 42 step=i; 43 } 44 } 45 } 46 int main() 47 { 48 int T; 49 long long ss,aa; 50 while(scanf("%d",&T) && T) 51 { 52 ss = 0;aa = 0; 53 for(int i=1;i<=T;i++) 54 for(int j=1;j<=T;j++) 55 scanf("%lld",&a[i][j]); 56 int mid1 = (T+1)/2; 57 for(int i=3,k=1;i<=T;i=i+2,k++) 58 { 59 step = 0;ans = 0; 60 solve1(i,mid1 - k,mid1-k,mid1-k,mid1+k,mid1+k,mid1+k,mid1+k,mid1-k);//顺时针 61 solve2(i,mid1 - k,mid1-k,mid1-k,mid1+k,mid1+k,mid1+k,mid1+k,mid1-k);//逆时针 62 ss= ss +step; 63 aa = ans+aa; 64 } 65 printf("%lld %lld\n",aa+a[mid1][mid1],ss); 66 } 67 return 0; 68 }
时间: 2024-12-10 04:17:57