2015
09-17

# Rotation Lock Puzzle

Alice was felling into a cave. She found a strange door with a number square matrix. These numbers can be rotated around the center clockwise or counterclockwise. A fairy came and told her how to solve this puzzle lock: “When the sum of main diagonal and anti-diagonal is maximum, the door is open.”.
Here, main diagonal is the diagonal runs from the top left corner to the bottom right corner, and anti-diagonal runs from the top right to the bottom left corner. The size of square matrix is always odd.

This sample is a square matrix with 5*5. The numbers with vertical shadow can be rotated around center ‘3’, the numbers with horizontal shadow is another queue. Alice found that if she rotated vertical shadow number with one step, the sum of two diagonals is maximum value of 72 (the center number is counted only once).

Multi cases is included in the input file. The first line of each case is the size of matrix n, n is a odd number and 3<=n<=9.There are n lines followed, each line contain n integers. It is end of input when n is 0 .

Multi cases is included in the input file. The first line of each case is the size of matrix n, n is a odd number and 3<=n<=9.There are n lines followed, each line contain n integers. It is end of input when n is 0 .

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

1 1 9 9 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        3.cpp
*  Create Date: 2013-09-08 14:21:58
*  Descripton:  simulate
*/

#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

const int MAXN = 100;
int a[MAXN][MAXN];
int n, sum, cnt;

void solve(int k, int l) {
int tmp = -0xffffff, tt = 0;
rep(i, l - 1) {
int t = a[k][k + i] + a[k + i][k + l - 1] + a[k + l - i - 1][k + 0] + a[k + l - 1][k + l - i - 1];
if (tmp <= t) {
if (tmp == t)
tt = min(tt, min(i, l - i - 1));
else
tt = min(i, l - i - 1);
tmp = t;
}
}
sum += tmp;
cnt += tt;
}

int main() {
while (scanf("%d", &n) && n) {
rep(i, n) rep(j, n)
scanf("%d", &a[i][j]);
int l = n / 2;
sum = a[l][l];
cnt = 0;
rep(i, l)
solve(i, n - 2 * i);
printf("%d %d\n", sum, cnt);
}
return 0;
}