Rotation Lock Puzzle
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 622Accepted Submission(s): 179
Problem Description
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).
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).
Input
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 .
Output
For each test case, output the maximum sum of two diagonals and minimum steps to reach this target in one line.
Sample Input
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
Sample Output
72 1
Source
Recommend
liuyiding
题解:
刚刚接触这道题时,我就想用BFS或者用DFS把每一种状态都枚举出来。。。。
枚举大约就8*16*24*32*(移矩阵的步数) 看一下时间应该不会超时。。
不过后来有分析了一下,它只是要你相加最大,所以每一层移位都是独立的。。
然后时间复杂度又到了(8+16+24+32)*(移矩阵的步数)
基本上最耗时间的就在移矩阵的步数。。
然后我又想了一下。。。例如3*3矩阵时,我移一步跟移三步的效果一样。。
然后又变成了(3+5+7+9)*(移矩阵的步数)
然后就到了最耗时间的移位。。
但是我们只是想要正方形四个角的数字之和。。就没必要移位。。只要每次都找到四个角就行了。。
但是可能我对矩阵的运用还不熟悉吧,居然想了很久。。。唉
然后时间复杂度就到了(3+5+7+9)*(里面的几个判断)。。
最后0ms通过。。。
哈哈。。神奇。。。。
#include<stdio.h>
#include<string.h>
#include<math.h>
int n,map[11][11];
int a[6][11];
int main()
{
int max,min;
while(scanf("%d",&n)&& n!=0)
{
int maxnum = 0;
int minpu = 0;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&map[i][j]);
int m = n-1;
int num = n;
for(int t=1;t<=n/2;t++)
{
max = 0;
min = 99999;
for(int s=0;s<m-(t-1)*2;s++)
{
a[t][s] = map[t][t+s] + map[t+s][num] +map[num][num-s] + map[num-s][t];
if(max<a[t][s])
{
max = a[t][s];
if(s+1>(num/2+1))
min = num-s-1;
else
min = s;
}
else if(max == a[t][s])
{
int Min;
if(s+1>(num/2+1))
Min = num-s-1;
else
Min = s;
if(min>Min)
min = Min;
}
}
num--;
maxnum+=max;
minpu+=min;
}
printf("%d %d\n",maxnum+map[n/2+1][n/2+1],minpu);
}
return 0;
}