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. JS,JQuery杂谈

    JS返回页面: JS返回前一个页面,经常看到有人用window.history.go(-1)这种方法  这种放的确可以返回,也仅仅只是返回,返回的页面信息却没有刷新.也有人用windows.histo ...

  2. OpenWrt启动过程分析

    openwrt是通过一系列shell脚本进行启动流程的组织,下面是启动流程的提纲.如 果想详细了解启动的过程,则需要仔细走读脚本文件. 1. 在make menuconfig 选择target平台 B ...

  3. Android学习笔记(广播机制)

    1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...

  4. iOS 延迟执行代码

    //延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)),dispatch_get_main ...

  5. 生成器模式(Builder)

    1.本质:分离整体构建算法和部分构造 2.示意图: 3.功能: 构建复杂的产品,而且是细化的.分步骤的构建产品 分离构建算法和具体的构建实现 具体的构造实现可以方便的切换和扩展 4.优点: 1.松散耦 ...

  6. 运用BeanUtils构建通用的查询 更新方法(个人拙作,不喜勿喷)

    ------------------------------------更新方法----------------------------------- public void update(Strin ...

  7. C++最后课程项目总结

    第一次独立完成的C++小项目,40小时 + 5小时Update + 8小时Linux移植. 过程: 过程非常认真,一个星期主要就是忙这个,为了完成某个部分,有时饭都推迟吃,连续对着电脑10几个小时很累 ...

  8. CentOS 忘记root密码,解决方法

    1.开机后,在倒数5秒结束前,按下任意键 2.在显示centos...的那个界面下,按e键(edit) 3.会出现三行的界面,选择中间 kernel...那行,然后按e键 4.在接着出现的那个界面最后 ...

  9. C#快速导入海量XML数据至SQL Server数据库

    #region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...

  10. 用jQuery实现瀑布流效果学习笔记

    jQuery一直没系统的学,只知道是js库,封装了好多js函数,方便了开发.以前做过一个原生的图片网站瀑布流效果,超级麻烦,这次用了jQuery方法,瞬间代码浓缩了,只有56行js代码.神奇的让我来把 ...