http://vjudge.net/contest/view.action?cid=53024#problem/C
Total Submission(s): 1421 Accepted Submission(s): 454
Rotation Lock Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1421 Accepted Submission(s): 454
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
这道题也是模拟,(旋转矩阵),思路就是模拟矩阵的旋转,我们只关注主对角线的两个端点和副对角线的两个端点,模拟的过程就是把把这四个端点的值用每个端点左右的值代替,由于对称性,旋转的时候只需旋转 ( n -2 * i +2 )/ 2+1次!#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int a[10][10];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(cin>>n&&n)
{
int step=0,value1=0,steps=0,value=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
int midvalue=a[(n+1)/2][(n+1)/2];
for(int i=1;i<=n/2;i++)///圈数
{
value=a[i][i]+a[n-i+1][n-i+1]+a[n-i+1][i]+a[i][n-i+1];step=0;
for(int j=1;j<=(n-2*i+2)/2+1;j++)///!!!
{
if(value<a[i][i+j]+a[n-i+1][n-i+1-j]+a[n-i+1-j][i]+a[i+j][n-i+1]) ///逆时针
{
value=a[i][i+j]+a[n-i+1][n-i+1-j]+a[n-i+1-j][i]+a[i+j][n-i+1];
step=j;
}
if(value<a[i+j][i]+a[n-i+1-j][n-i+1]+a[n-i+1][i+j]+a[i][n-i+1-j])
{
value=a[i+j][i]+a[n-i+1-j][n-i+1]+a[n-i+1][i+j]+a[i][n-i+1-j];
step=j;
}
}
value1+=value;
steps+=step;
}
cout<<value1+midvalue<<" "<<steps<<endl;
}
return 0;
}