HDUOJ---(4708)Rotation Lock Puzzle
Rotation Lock Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 695 Accepted Submission(s): 204
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. 9 3 2 5 9 7 4 7 5 4 6 9 3 9 3 5 2 8 7 2 9 9 4 1 9This 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 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 int min(int a,int b)
5 {
6 return a<b?a:b;
7 }
8 int main()
9 {
10 int n,i,j,max,pos,tpos;
11 int str[10][10],sum;
12 while(cin>>n,n)
13 {
14 for(i=1;i<=n;i++) //输入部分
15 {
16 for(j=1;j<=n;j++)
17 scanf("%d",str[i]+j);
18 }
19 pos=0;
20 max=str[(n+1)/2][(n+1)/2];
21 for(i=1 ; i<=(n-1)/2 ; i++) //圈数
22 {
23 for(j=i;j<=n-i;j++)
24 {
25 int temp=(str[j][i]+str[n-j+1][n-i+1])+(str[n-i+1][j]+str[i][n-j+1]); //主副对角线相加
26 if(j==i||sum<temp)
27 sum=temp,tpos=min(j-i,n-(i+j)+1);
28 }
29 pos+=tpos;
30 max+=sum;
31 }
32 printf("%d %dn",max,pos);
33 }
34 return 0;
35 }
- 【Python学习笔记之三】lambda表达式用法小结
- Selenium2+python自动化58-读取Excel数据(xlrd)
- 软件测试金字塔
- Selenium2+python自动化59-数据驱动(ddt)
- 浅谈我对动态规划的一点理解---大家准备好小板凳,我要开始吹牛皮了~~~
- Selenium2+python自动化60-异常后截图(screenshot)
- Cisco Packet Tracer 6.0 实验笔记
- kali 2018.1安装教程
- python接口自动化2-发送post请求
- TypeScript 动态创建类
- Java学习笔记【持续更新】
- 互联网协议入门(二)
- 设计模式六大原则(4):接口隔离原则
- 设计模式六大原则(3):依赖倒置原则
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法