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
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=;
const int maxm= + ; int n, M[maxn][maxn];
int d[maxn][maxm]; //d[i][j]表示从外往里数第i层,从左上角开始,下标从0开始,顺时针第j个是谁
int Max[maxn]; //Max[i]表示第i层对角线的4个数的和的最大值
int r[maxn]; //r[i]表示第i层转到对角线的4个数的和的最大值时转了多少次 void read()
{
int i,j;
for(i= ; i<n; i++)
for(j=; j<n; j++)
scanf("%d", &M[i][j]);
} void get_d()
{
int i,j,m;
for(i= ; i<n/; i++)
{
m=;
for(j=i; j<n-i; j++) d[i][m++]=M[i][j];
for(j=i+; j<n-i; j++) d[i][m++]=M[j][n--i];
for(j=n--i; j>=i; j--) d[i][m++]=M[n--i][j];
for(j=n--i; j>i; j--) d[i][m++]=M[j][i];
}
} void get_Mr()
{
int i,j;
for(i= ; i<n/; i++)
{
int N=(n - i*) -;
int Ma=-;
for(j=; j<N; j++) //转j次
{
int temp=d[i][j] + d[i][j+N] + d[i][j+*N] + d[i][j+*N];
if(temp>Ma)
{
Ma=temp;
Max[i]=temp;
r[i]=min(j, N-j);
}
else if(temp == Ma)
{
r[i]=min(r[i], j);
r[i]=min(r[i], N-j);
}
}
}
} void solve()
{
int sum_val=, sum_rot=,i;
for(i= ; i<n/; i++) sum_val += Max[i];
sum_val += M[n/][n/];
for(i= ; i<n/; i++) sum_rot += r[i];
printf("%d %d\n", sum_val, sum_rot);
} int main()
{
while(scanf("%d", &n) == && n)
{
read();
get_d();
get_Mr();
solve();
}
return ;
}

Rotation Lock Puzzle的更多相关文章

  1. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  2. HDU 4708:Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. hdu4708 Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  4. HDU 4708 Rotation Lock Puzzle (简单题)

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. HDUOJ---(4708)Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. HDU 4708 Rotation Lock Puzzle(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...

  7. hdu 4708 Rotation Lock Puzzle 2013年ICPC热身赛A题 旋转矩阵

    题意:给出一个n*n的矩阵,旋转每一圈数字,求出对角线可能的最大值,以及转到最大时的最小距离. 只要分析每一层就可以了,本来想用地址传递二维数组,发现行不通,改了一下就行了. 这里有个坑,比如: 1 ...

  8. Codeforces Round #467 (Div. 2) E -Lock Puzzle

    Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...

  9. Codeforces Round #467 (Div. 1). C - Lock Puzzle

    #include <algorithm> #include <cstdio> #include <cstring> #include <iostream> ...

随机推荐

  1. LinkedIn第三方登录

    官方开发文档网址:https://developer.linkedin.com angularjs LinkedIn初始化 var apiKey='77n7z65hd7azmb';$(function ...

  2. angularjs-ngTable select filter

    jsp <td title="'Status'" filter="{status: 'select'}" filter-data="fn.sta ...

  3. CentOS7下安装SVN服务端

    CentOS7下安装SVN服务 1. yum命令即可方便的完成安装# sudo yum install subversion 测试安装是否成功:# svnserve --version 更改svn的默 ...

  4. chapter1-开始(1)

    C++学习小记 之前“看”过C++,但是纯粹只是为了应付考试.现在想重新学习,久仰<C++ primer>大名,书之厚令我生畏,好记性不如烂笔头,遂以博客形式笔记之. 本人编程菜鸟一枚,当 ...

  5. 292. Nim Game(C++)

    292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...

  6. JavaScript多线程初步学习

    一.多线程理解 首先,我们要理解什么是多线程,百度百科上说:多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一 ...

  7. cocod2d-x 之 CCTMXTiledMap & CCTMXLayer

    cocos2dx框架自带的地图CCTMXTiledMap,继承自CCNode.CCTMXTiledMap的坐标系的原点位于左上角,以一个瓦片为单位,换句话说,左上角第一块瓦片的坐标为(0,0),而紧挨 ...

  8. Ubuntu 12.04 下安装配置 JDK 7(tar)

    第一步:下载jdk-7u45-linux-i586.tar.gz 到Orcale的JDK官网下载JDK7的tar包 第二步:解压安装 tar -zxvf ./jdk-7u45-linux-i586.t ...

  9. 入门1:PHP的优点

    一.语法简单 二.学习成本低 (因为语法简单 所以学习成本低) 三.开发效率高 (PHP运行流程很简单,语法也很简单,必然开发效率就高) 四.跨平台 (我们只需要写一份PHP的程序,就可以非常方便的把 ...

  10. 学习WindowsPhone 2013/12/22

    菜鸟一枚,只能边看别人的博客变学习来提升自己,参考博客内容:http://blog.csdn.net/column/details/wp-comming.html?page=3 ,稍微看了一下,写的还 ...