对每个环都进行遍历,找出最大值
如果值相等的话就可以还要判断下走的步数
5 9 3 2 5 9 7 4 7 5 4 6 9 3 9 3 5 2 8 7 2 9 9 4 1 9 0
72 1
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> #include <iostream> #include <queue> #include <stack> #include <deque> #include <vector> #include <set> #include <map> using namespace std; #define MAXN 12 int num[12][12]; int main(){ int n,i,j,k; while(~scanf("%d",&n)){ if(n == 0){ break; } for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&num[i][j]); } } int ans = 0; int anscon = 0; for(k=1;k<=n/2;k++){ int marksum = -1; int mark = k; int sum=0; for(i=k;i<=n-k;i++){ sum = 0; //printf("%d %d %d %d\n",num[k][i],num[i][n-k+1],num[n-k+1][n-i+1],num[n-i+1][k]); sum+=num[k][i]; sum+=num[i][n-k+1]; sum+=num[n-k+1][n-i+1]; sum+=num[n-i+1][k]; //printf("%d\n",sum); if(sum > marksum){ marksum = sum; mark = min(i-k,(n-k+1-i)); } if(sum == marksum){ int mark1 = min(i-k,n-k+1-i); mark = min(mark1,mark); } } //printf("%d\n",num[n/2+1][n/2+1]); ans+=marksum; anscon+=mark; } printf("%d %d\n",ans+num[n/2+1][n/2+1],anscon); } return 0; }
原文:http://blog.csdn.net/zcr_7/article/details/41450633