HDUOJ---(4708)Rotation Lock Puzzle

Rotation Lock Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 695    Accepted Submission(s): 204

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.

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
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
旋转.....用分治做或许更好,但是为了理解今天特地用模拟做了一次.....
代码如下:
 1 #include<iostream>

 2 #include<cstdio>

 3 using namespace std;

 4 int min(int a,int b)

 5 {

 6     return a<b?a:b;

 7 }

 8 int main()

 9 {

10     int n,i,j,max,pos,tpos;

11     int str[10][10],sum;

12   while(cin>>n,n)

13   {

14       for(i=1;i<=n;i++)        //输入部分

15       {

16           for(j=1;j<=n;j++)

17               scanf("%d",str[i]+j);

18       }

19        pos=0;

20        max=str[(n+1)/2][(n+1)/2];

21       for(i=1 ; i<=(n-1)/2 ; i++)    //圈数

22       {

23          for(j=i;j<=n-i;j++)

24          {

25              int temp=(str[j][i]+str[n-j+1][n-i+1])+(str[n-i+1][j]+str[i][n-j+1]);  //主副对角线相加

26              if(j==i||sum<temp)

27                  sum=temp,tpos=min(j-i,n-(i+j)+1);

28          }

29          pos+=tpos;

30          max+=sum;

31       }

32      printf("%d %d\n",max,pos);

33   }

34     return 0;

35 }
View Code

 

你可能感兴趣的:(Lock)