前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ---(4708)Rotation Lock Puzzle

HDUOJ---(4708)Rotation Lock Puzzle

作者头像
Gxjun
发布2018-03-21 12:49:17
4320
发布2018-03-21 12:49:17
举报
文章被收录于专栏:ml

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

旋转.....用分治做或许更好,但是为了理解今天特地用模拟做了一次.....

代码如下:

代码语言:javascript
复制
 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 %d\n",max,pos);
33   }
34     return 0;
35 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-09-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Rotation Lock Puzzle
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档