Rotation Lock Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 290 Accepted Submission(s): 60
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).
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
2013 ACM/ICPC Asia Regional Online —— Warmup
Recommend liuyiding
按照题目意思去旋转。
求最大值
1 /* ******************************************* 2 Author : kuangbin 3 Created Time : 2013年09月08日 星期日 12时33分12秒 4 File Name : 1003.cpp 5 ******************************************* */ 6 7 #include <stdio.h> 8 #include <algorithm> 9 #include <iostream> 10 #include <string.h> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 21 long long a[20][20]; 22 int n; 23 void change(int &x,int &y,int k) 24 { 25 if(x == k) 26 { 27 if(y == k) 28 { 29 x++; 30 } 31 else y--; 32 } 33 else if(x == n+1-k) 34 { 35 if(y == n+1-k) 36 x--; 37 else y++; 38 } 39 else if(y == k) 40 x++; 41 else x--; 42 } 43 int main() 44 { 45 // freopen("in.txt","r",stdin); 46 // freopen("out.txt","w",stdout); 47 while(scanf("%d",&n) && n) 48 { 49 for(int i = 1;i <= n;i++) 50 for(int j = 1;j <= n;j++) 51 cin>>a[i][j]; 52 long long ans1 = 0, ans2 = 0; 53 for(int i = 1;i <= n/2;i++) 54 { 55 int x0 = i,y0 = i; 56 int x1 = i,y1 = n+1-i; 57 int x2 = n+1-i,y2 = i; 58 int x3 = n+1-i,y3 = n+1-i; 59 int tmp1 = a[x0][y0] + a[x1][y1] + a[x2][y2] + a[x3][y3]; 60 int tmp2 = 0; 61 for(int j = 0;j < (n+2-2*i-2);j++) 62 { 63 change(x0,y0,i); 64 change(x1,y1,i); 65 change(x2,y2,i); 66 change(x3,y3,i); 67 int tt = min(j+1,n+2-2*i-1-(j+1)); 68 if(tmp1 < a[x0][y0] + a[x1][y1] + a[x2][y2] + a[x3][y3]) 69 { 70 tmp1 = a[x0][y0] + a[x1][y1] + a[x2][y2] + a[x3][y3]; 71 tmp2 = tt; 72 } 73 else if(tmp1 == a[x0][y0] + a[x1][y1] + a[x2][y2] + a[x3][y3] && tmp2 > tt) 74 { 75 tmp2 = tt; 76 } 77 } 78 ans1 += tmp1; 79 ans2 += tmp2; 80 } 81 ans1 += a[n/2+1][n/2+1]; 82 cout<<ans1<<" "<<ans2<<endl; 83 } 84 return 0; 85 }