Rotation Lock Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 571    Accepted Submission(s): 153

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
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<queue>
  7. #include<algorithm>
  8. #include<iomanip>
  9. #define INF 99999999
  10. using namespace std;
  11.  
  12. const int MAX=10;
  13. int s[MAX][MAX];
  14.  
  15. int main(){
  16. int n;
  17. while(scanf("%d",&n),n){
  18. for(int i=0;i<n;++i){
  19. for(int j=0;j<n;++j)scanf("%d",&s[i][j]);
  20. }
  21. int sum=0,num=0,temp=0,k;
  22. for(int i=0;i<n/2;++i){//从第i行开始的四边形
  23. k=0,temp=-INF;
  24. for(int j=0;j<=n-2*i-2;++j){//分别计算顺时针旋转j(逆时针旋转n-2*i-1-j次)次后左上角,左下角,右上角,右下角相加的和
  25. if(s[i+j][i]+s[n-1-i][i+j]+s[i][n-1-i-j]+s[n-1-i-j][n-1-i]>temp){
  26. temp=s[i+j][i]+s[n-1-i][i+j]+s[i][n-1-i-j]+s[n-1-i-j][n-1-i];
  27. k=j;
  28. }
  29. }
  30. sum+=temp;
  31. num+=min(k,n-2*i-1-k);
  32. }
  33. cout<<sum+s[n/2][n/2]<<' '<<num<<endl;
  34. }
  35. return 0;
  36. }

hdu4708的更多相关文章

  1. hdu4708 Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. 在Windows下github展示代码

    最近大爱Web编程,于是寻找各种代码中,然后就发现了GitHub这个网站,如果你知道Google Code,那么你就知道这个GitHub是做什么的了.不过GitHub主要是用作基于Git的分布式版本管 ...

  2. HDU 3916 Sequence Decomposition 【贪心】

    这道题目的题意就是使用题目中所给的Gate 函数,模拟出输入的结果 当然我们分析的时候可以倒着来,就是拿输入去减 每次Gate 函数都会有一个有效范围 这道题目求的就是,找出一种模拟方法,使得最小的有 ...

  3. DFS 练习 (这篇真的是随笔)

    目的: 输入: 3 输出: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 代码如下: #include<stdio.h> ],b[],n; void dfs(in ...

  4. MessageBox不能前置显示的问题

    在MFC的开发中,经常会遇到一些莫名奇妙的问题,可能是经验不足的原因吧. 进入正题....在手头的项目中,用MFC做的界面应用.在某一天突然发现程序界面不能进行响应,经过反复的调试后发现:Messag ...

  5. shiro权限框架

    权限的组成部分:用户 资源 角色 权限 数据库关系表设计是根据自己项目需求设计的 account表role表(id,rolename)account_role(id,aid,rid)permissio ...

  6. Android编程心得-设计一个可重用的自定义Dialog

            我们在实际开发过程中,会遇到一个问题,我们的Dialog如果使用一般的方法进行设置调用出来,会有很多的重复代码,如何将Dialog按照自己的思路设计呢,并让其可重用呢,下面我来介绍一下 ...

  7. lua序列化table表到文件中

    先上代码 function luautil.serialize(t, sort_parent, sort_child) local mark={} local assign={} local func ...

  8. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  9. MSSQL奇技淫巧

    MSSQL:获得库每个表的记录数和容量 sp_msforeachtable是MS未公开的存储过程: exec sp_msforeachtable @command1="print '?'&q ...

  10. ELK 之一:ElasticSearch 基础和集群搭建

    一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...