HDU 4708 Rotation Lock Puzzle
来源:互联网 发布:linux服务器禁端口扫描 编辑:程序博客网 时间:2024/05/20 01:47
Rotation Lock Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 659 Accepted Submission(s): 193
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
59 3 2 5 97 4 7 5 46 9 3 9 35 2 8 7 29 9 4 1 90
Sample Output
72 1
Source
2013 ACM/ICPC Asia Regional Online —— Warmup
import java.io.*;import java.math.BigInteger;import java.util.*;public class Main {BufferedReader bu;PrintWriter pw;int n;int[][] a;public static void main(String[] args) throws IOException {new Main().work();}void work() throws IOException {bu = new BufferedReader(new InputStreamReader(System.in));pw = new PrintWriter(new OutputStreamWriter(System.out),true);n = Integer.parseInt(bu.readLine());while (n != 0) {a = new int[n][n];for (int i = 0; i < n; i++) {String str[] = bu.readLine().split(" ");for (int j = 0; j < n; j++) {a[i][j] = Integer.parseInt(str[j]);}}BigInteger result = BigInteger.ZERO;int step = 0;for (int i = 0; i < n / 2; i++) {//控制圈数BigInteger max = BigInteger.ZERO;int temp = 0;for (int j = i; j < n - 1 - i; j++) {BigInteger sum = BigInteger.ZERO;sum = sum.add(BigInteger.valueOf(a[i][j]));//第i圈的 第一行顺时针相加sum = sum.add(BigInteger.valueOf(a[n - 1 - i][n - 1 - j]));//第i圈的最后一行逆时针相加sum = sum.add(BigInteger.valueOf(a[j][n - 1 - i]));//第i圈的最后一列,顺时针相加sum = sum.add(BigInteger.valueOf(a[n - 1 - j][i]));//第i圈的第一列,逆时针相加int flag = Math.min(Math.abs(j - i),Math.abs(j - (n - 1 - i)));//每次转圈的最小步数 if (sum.compareTo(max) > 0) {max = sum;temp = flag;} else if (sum.compareTo(max) == 0 && temp > flag) {temp = flag;}}step += temp;result = result.add(max);}result = result.add(BigInteger.valueOf(a[n / 2][n / 2]));//最后再加上中心元素pw.println(result + " " + step);n = Integer.parseInt(bu.readLine());}}}
- hdu - 4708 - Rotation Lock Puzzle
- hdu 4708 Rotation Lock Puzzle
- HDU 4708 Rotation Lock Puzzle
- hdu 4708 Rotation Lock Puzzle
- HDU 4708 Rotation Lock Puzzle
- HDU 4708 Rotation Lock Puzzle
- HDU 4708 Rotation Lock Puzzle
- hdu 4708 Rotation Lock Puzzle
- HDU 4708:Rotation Lock Puzzle
- hdu 4708 Rotation Lock Puzzle(模拟)
- HDU 4708 Rotation Lock Puzzle 解题报告
- Hdu 4708 Rotation Lock Puzzle --模拟
- HDU 4708 Rotation Lock Puzzle (热身赛第三题)
- HDU 4708 Rotation Lock Puzzle(数学啊)
- hdu4708 Rotation Lock Puzzle
- HDU 4708 Rotation Lock Puzzle && 2013 ACM/ICPC Asia Regional Online —— Warmup
- hdu 4708 Rotation Lock Puzzle 2013年ICPC热身赛A题 旋转矩阵
- 2013杭电warm up1 Rotation Lock Puzzle
- winform中为ComboBox控件添加“请选择”或“全部”选项
- C/C++语言中闭包的探究及比较
- linux下的C语言多线程编程小例子
- 关于联合体union的详细解释
- 编译redis-2.6.14出现"Newer version of jemalloc required",解决办法
- HDU 4708 Rotation Lock Puzzle
- oracle ceil和floor函数
- GPRS 中的PDP上下文名词解释
- 如何在github中贡献自己的力量
- HDU1150-Machine Schedule
- 给定一个字符串,输出其所有的可能排列
- Kombu 基础
- Visual Assist X Options(VAssistX)如何修改倾斜
- kermit代替minicom