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. JAVA 生成PDF报表()

    许多应用程序都要求动态生成 PDF 文档.这些应用程序涵盖从生成客户对帐单并通过电子邮件交付的银行到购买特定的图书章节并以 PDF 格式接收这些图书章节的读者.这个列表不胜枚举.在本文中,我们将使用 ...

  2. [转]Web UI 设计命名规范

    来源:http://blog.bingo929.com/web-ui-design-name-convention.html 一.网站设计及基本框架结构: 1.    Container “conta ...

  3. .net中使用JQuery Ajax判断用户名是否存在的方法

    //第一步:新建一个(*.aspx|*.html)Index.aspx页面 添加jquery 1 <html xmlns="http://www.w3.org/1999/xhtml&q ...

  4. 40个DBA日常维护的SQL脚本--1113

    from itpub --1.查询碎片程度高的表--条件为什么block>100,因为一些很小的表,只有几行数据实际大小很小,但是block一次性分配就是5个(11g开始默认一次性分配1M的bl ...

  5. OC - 19.GCD

    简介 GCD(Grand Center Dispatch)是Apple为多核的并行运算提出的解决方案,纯C语言 更加适配多核处理器,且自动管理线程的生命周期,使用起来较为方便 GCD通过任务和队列实现 ...

  6. 使用SQL Server 2008远程链接时SQL数据库不成功的解决方法

    关键设置: 第一步(SQL2005.SQL2008): 开始-->程序-->Microsoft SQL Server 2008(或2005)-->配置工具-->SQL Serv ...

  7. 读书笔记之 - javascript 设计模式 - 组合模式

    组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...

  8. VLC命令参数(转载)

    转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...

  9. BAE Flask UEditor 使用七牛云

    1. 配置BAE支持七牛云的SDK BAE的python requirements当然不支持竞争对手了. 解决方法: 把qiniu这个文件包直接放置在你项目的目录中(与其他app同级) 运行会发现缺少 ...

  10. TRECT的使用

    作为一张画布,在上面绘制各种图形或显示图像,但在CANVAS的使用过程中少不了一个特殊对象,那就是矩形RECT,灵活使用它会完成很多特殊的功能,为Delphi编制的Windows程序增加活力. REC ...