zoukankan      html  css  js  c++  java
  • HDU 4708 Rotation Lock Puzzle

    Rotation Lock Puzzle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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 <iostream>
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <fstream>
    #include <vector>
    #define Min(a,b) ((a)<(b)?(a):(b))
    #pragma comment(linker, "/STACK:16777216")
    using namespace std ;
    typedef long long LL ;
    int A[2][2] ,nowA[2][2];
    int B[4][4] ,nowB[4][4] ;
    int C[6][6] ,nowC[6][6] ;
    int D[8][8] ,nowD[8][8] ;
    int E[10][10] ,nowE[10][10];
    void rotated_B(int T){
        int num[100] ;
        int id=0 ,i ,j;
        for(i=1;i<=3;i++)
            num[++id]=B[1][i] ;
        for(i=2;i<=3;i++)
            num[++id]=B[i][3] ;
        for(i=2;i>=1;i--)
            num[++id]=B[3][i] ;
        for(i=2;i>1;i--)
            num[++id]=B[i][1] ;
        while(T--){
            int x=num[id] ;
            for(int i=id;i>=2;i--)
                num[i]=num[i-1] ;
            num[1]=x ;
        }
        int k=1 ;
        for(i=1;i<=3;i++)
            nowB[1][i]=num[k++] ;
        for(i=2;i<=3;i++)
            nowB[i][3]=num[k++] ;
        for(i=2;i>=1;i--)
            nowB[3][i]=num[k++] ;
        for(i=2;i>1;i--)
            nowB[i][1]=num[k++] ;
    }
    void rotated_C(int T){
        int num[200] ;
        int id=0 ,i ,j;
        for(i=1;i<=5;i++)
            num[++id]=C[1][i] ;
        for(i=2;i<=5;i++)
            num[++id]=C[i][5] ;
        for(i=4;i>=1;i--)
            num[++id]=C[5][i] ;
        for(i=4;i>1;i--)
            num[++id]=C[i][1] ;
        while(T--){
            int x=num[id] ;
            for(int i=id;i>=2;i--)
                num[i]=num[i-1] ;
            num[1]=x ;
        }
        int k=1 ;
        for(i=1;i<=5;i++)
            nowC[1][i]=num[k++] ;
        for(i=2;i<=5;i++)
            nowC[i][5]=num[k++] ;
        for(i=4;i>=1;i--)
            nowC[5][i]=num[k++] ;
        for(i=4;i>1;i--)
            nowC[i][1]=num[k++] ;
    }
    void rotated_D(int T){
        int num[300] ;
        int id=0 ,i ,j;
        for(i=1;i<=7;i++)
            num[++id]=D[1][i] ;
        for(i=2;i<=7;i++)
            num[++id]=D[i][7] ;
        for(i=6;i>=1;i--)
            num[++id]=D[7][i] ;
        for(i=6;i>1;i--)
            num[++id]=D[i][1] ;
        while(T--){
            int x=num[id] ;
            for(int i=id;i>=2;i--)
                num[i]=num[i-1] ;
            num[1]=x ;
        }
        int k=1 ;
        for(i=1;i<=7;i++)
            nowD[1][i]=num[k++] ;
        for(i=2;i<=7;i++)
            nowD[i][7]=num[k++] ;
        for(i=6;i>=1;i--)
            nowD[7][i]=num[k++] ;
        for(i=6;i>1;i--)
            nowD[i][1]=num[k++] ;
    }
    void rotated_E(int T){
        int num[400] ;
        int id=0 ,i ,j;
        for(i=1;i<=9;i++)
            num[++id]=E[1][i] ;
        for(i=2;i<=9;i++)
            num[++id]=E[i][9] ;
        for(i=8;i>=1;i--)
            num[++id]=E[9][i] ;
        for(i=8;i>1;i--)
            num[++id]=E[i][1] ;
        while(T--){
            int x=num[id] ;
            for(int i=id;i>=2;i--)
                num[i]=num[i-1] ;
            num[1]=x ;
        }
        int k=1 ;
        for(i=1;i<=9;i++)
            nowE[1][i]=num[k++] ;
        for(i=2;i<=9;i++)
            nowE[i][9]=num[k++] ;
        for(i=8;i>=1;i--)
            nowE[9][i]=num[k++] ;
        for(i=8;i>1;i--)
            nowE[i][1]=num[k++] ;
    }
    int sum3(){
        return nowB[1][1]+nowB[3][3]+nowB[3][1]+A[1][1]+nowB[1][3] ;
    }
    void gao3(){
       int ans=B[1][1]+B[3][3]+B[3][1]+A[1][1]+B[1][3] ;
       int step=100000;
       for(int i=0;i<8;i++){
           rotated_B(i) ;
           int sum=sum3() ;
           if(sum>ans){
                step=Min(i,8-i) ;
                ans=sum ;
           }
           else if(sum==ans)
                step=Min(step ,Min(i,8-i)) ;
       }
       printf("%d %d
    ",ans,step) ;
    }
    int sum5(){
        return nowB[1][1]+nowB[3][3]+nowB[3][1]+A[1][1]+nowB[1][3]
               +nowC[1][1]+nowC[5][5]+nowC[5][1]+nowC[1][5] ;
    }
    void gao5(){
       int ans=B[1][1]+B[3][3]+B[3][1]+A[1][1]+B[1][3]
               +C[1][1]+C[5][5]+C[5][1]+C[1][5] ;
       int step=100000;
       for(int i=0;i<8;i++)
        for(int j=0;j<16;j++){
           rotated_B(i) ;
           rotated_C(j) ;
           int sum=sum5() ;
           if(sum>ans){
                step=Min(i,8-i)+Min(j,16-j) ;
                ans=sum ;
           }
           else if(sum==ans)
                step=Min(step,(Min(i,8-i)+Min(j,16-j))) ;
       }
       printf("%d %d
    ",ans,step) ;
    }
    int sum7(){
        return nowB[1][1]+nowB[3][3]+nowB[3][1]+A[1][1]+nowB[1][3]
               +nowC[1][1]+nowC[5][5]+nowC[5][1]+nowC[1][5]
               +nowD[1][1]+nowD[7][7]+nowD[7][1]+nowD[1][7];
    }
    void gao7(){
       int ans=B[1][1]+B[3][3]+B[3][1]+A[1][1]+B[1][3]
               +C[1][1]+C[5][5]+C[5][1]+C[1][5]
               +D[1][1]+D[7][7]+D[7][1]+D[1][7];
       int step=100000;
       for(int i=0;i<8;i++)
         for(int j=0;j<16;j++)
           for(int k=0;k<24;k++){
               rotated_B(i) ;
               rotated_C(j) ;
               rotated_D(k) ;
               int sum=sum7() ;
               if(sum>ans){
                    step=Min(i,8-i)+Min(j,16-j)+Min(k,24-k) ;
                    ans=sum ;
               }
               else if(sum==ans)
                    step=Min(step,(Min(i,8-i)+Min(j,16-j)+Min(k,24-k))) ;
       }
       printf("%d %d
    ",ans,step) ;
    }
    int sum9(){
        return nowB[1][1]+nowB[3][3]+nowB[3][1]+A[1][1]+nowB[1][3]
               +nowC[1][1]+nowC[5][5]+nowC[5][1]+nowC[1][5]
               +nowD[1][1]+nowD[7][7]+nowD[7][1]+nowD[1][7]
               +nowE[1][1]+nowE[9][9]+nowE[9][1]+nowE[1][9];
    }
    void gao9(){
       int ans=B[1][1]+B[3][3]+B[3][1]+A[1][1]+B[1][3]
               +C[1][1]+C[5][5]+C[5][1]+C[1][5]
               +D[1][1]+D[7][7]+D[7][1]+D[1][7]
               +E[1][1]+E[9][9]+E[9][1]+E[1][9] ;
       int step=100000;
       for(int i=0;i<8;i++)
         for(int j=0;j<16;j++)
           for(int k=0;k<24;k++)
             for(int m=0;m<32;m++){
               rotated_B(i) ;
               rotated_C(j) ;
               rotated_D(k) ;
               rotated_E(m) ;
               int sum=sum9() ;
               if(sum>ans){
                    step=Min(i,8-i)+Min(j,16-j)+Min(k,24-k)+Min(m,32-m);
                    ans=sum ;
               }
               else if(sum==ans)
                    step=Min(step,(Min(i,8-i)+Min(j,16-j)+Min(k,24-k)+Min(m,32-m))) ;
       }
       printf("%d %d
    ",ans,step) ;
    }
    int main(){
        int n ;
        while(cin>>n&&n){
              if(n==3){
                  for(int i=1;i<=3;i++)
                    for(int j=1;j<=3;j++)
                       scanf("%d",&B[i][j]) ;
                  A[1][1]=B[2][2] ;
                  gao3() ;
              }
              else if(n==5){
                  for(int i=1;i<=5;i++)
                    for(int j=1;j<=5;j++)
                       scanf("%d",&C[i][j]) ;
                  for(int i=1;i<=3;i++)
                    for(int j=1;j<=3;j++)
                        B[i][j]=C[i+1][j+1] ;
                  A[1][1]=B[2][2] ;
                  gao5() ;
              }
              else if(n==7){
                  for(int i=1;i<=7;i++)
                    for(int j=1;j<=7;j++)
                       scanf("%d",&D[i][j]) ;
                  for(int i=1;i<=5;i++)
                    for(int j=1;j<=5;j++)
                       C[i][j]=D[i+1][j+1] ;
                  for(int i=1;i<=3;i++)
                    for(int j=1;j<=3;j++)
                        B[i][j]=C[i+1][j+1] ;
                  A[1][1]=B[2][2] ;
                  gao7() ;
              }
              else if(n==9){
                 for(int i=1;i<=9;i++)
                    for(int j=1;j<=9;j++)
                       scanf("%d",&E[i][j]) ;
                  for(int i=1;i<=7;i++)
                    for(int j=1;j<=7;j++)
                       D[i][j]=E[i+1][j+1] ;
                  for(int i=1;i<=5;i++)
                    for(int j=1;j<=5;j++)
                       C[i][j]=D[i+1][j+1] ;
                  for(int i=1;i<=3;i++)
                    for(int j=1;j<=3;j++)
                       B[i][j]=C[i+1][j+1] ;
                  A[1][1]=B[2][2] ;
                  gao9() ;
              }
        }
        return 0 ;
    }
    

      

  • 相关阅读:
    centos 7 pip install MySQL-python 报错
    修改centos history记录数上限
    CentOS 7 如何设置为eth0网卡
    字符串判空有空格报错:binary operator expected
    Linux指定用户运行程序
    MySQL 新建用户,为用户授权,指定用户访问数据库
    解决linux 中文乱码
    UNIX目录访问操作
    通过lseek产生空洞文件
    lseek系统调用
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3308607.html
Copyright © 2011-2022 走看看