题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708
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<cstdio> int minn(int a,int b) { return a<b?a:b; } int main() { int n; int a[17][17]; while(~scanf("%d",&n) && n) { int sum = 0, pos = 0; int i, j; int mid = (n+1)/2; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { scanf("%d",&a[i][j]); if(i==mid && j==mid) { sum = a[i][j]; } } } int tsum = 0, tpos = 0; for(i = 1; i <= (n-1)/2; i++) //层数 { for(j = i; j <= n-i; j++) { int tt=(a[j][i]+a[n-j+1][n-i+1])+(a[n-i+1][j]+a[i][n-j+1]); //主副对角线相加 if(j==i || tsum<tt) { tsum = tt; tpos = minn(j-i,n-(i+j)+1);//逆时针,顺时针 } } pos+=tpos; sum+=tsum; } printf("%d %d\n",sum,pos); } return 0; }
HDU 4708 Rotation Lock Puzzle(数学啊)
原文地址:http://blog.csdn.net/u012860063/article/details/41414231