Rotation Lock Puzzle
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1668 Accepted Submission (s): 530
Problem Descriptionalice 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 about to solve this puzzle lock: "When the sum of main diagonal and anti-diagonal is maximum, the Door is open. ".
Here, main diagonal are the diagonal runs from the top left corner to the bottom right corner, and Anti-diagonal runs from The top right and the bottom left corner. The size of the square matrix is always odd.
This sample is a square matrix with 5*5. The numbers with vertical shadow can being 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 the diagonals is maximum value of Center number is counted only once).
Inputmulti cases is included in the input file. The first line of this is the size of the matrix N, N was a odd number and 3<=n<=9.there are n lines followed, each Line contain n integers. It is the end of input when n is 0.
Outputfor each test case, output the maximum sum of one of the diagonals and minimum steps to reach this target on one line.
Sample Input59 3 2 5 97 4 7 5 46 9 3 9 35 2 8 7 29 9 4 1 90
Sample Output72 1
Source2013 ACM/ICPC Asia Regional online--warmup test instructions: a square matrix with an odd N (3<=n<=9) edge, where the small square matrix can be rotated clockwise or counterclockwise around the center, The maximum sum, and the minimum number of rotations, of the positive and secondary diagonal lines after the rotation. Analysis: Each layer is enumerated in turn, in each layer, the number of four points to reach the diagonal and the maximum, and then record the maximum value of each layer of rotation of how many times.
#pragmaCOMPRINT (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<time.h>#include<algorithm>#defineLL __int64#defineFIN freopen ("In.txt", "R", stdin)using namespacestd;Const intinf=0x3f3f3f3f;intMain () {intn,ans,anst,t,t1,sum,s; intval[ the][ the]; while(SCANF ("%d", &n) &&N) {anst=ans=0; Memset (Val,0,sizeof(Val)); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&Val[i][j]); for(intI=1; i<=n/2; i++) {sum=-INF; for(intj=0; j<n-(I-1)*2-1; j + +) {s=val[i][i+j]+val[i+j][n-i+1]+val[n-i+1][n-i+1-j]+val[n-i+1-J] [i]; if(sum<s) {sum=s; T1=j< (n-i-j)? J: (n-i-j); } Else if(sum==s) {T=j< (n-i-j)? J: (n-i-j); if(t<t1) T1=T; }} ans+=sum; Anst+=T1; } ans+=val[n/2+1][n/2+1]; printf ("%d%d\n", Ans,anst); } return 0;}
View Code
HDU 4708 Rotation Lock Puzzle (greedy + analog)