• 2016年第七届蓝桥杯C/C++ C组国赛 —— 第二题:反幻方


    反幻方

    我国古籍很早就记载着

    2 9 4
    7 5 3
    6 1 8

    这是一个三阶幻方。每行每列以及对角线上的数字相加都相等。

    下面考虑一个相反的问题。
    可不可以用 1~9 的数字填入九宫格。
    使得:每行每列每个对角线上的数字和都互不相等呢?

    这应该能做到。
    比如:
    9 1 2
    8 4 3
    7 5 6

    你的任务是搜索所有的三阶反幻方。并统计出一共有多少种。
    旋转或镜像算同一种。

    比如:
    9 1 2
    8 4 3
    7 5 6

    7 8 9
    5 4 1
    6 3 2

    2 1 9
    3 4 8
    6 5 7

    等都算作同一种情况。

    请提交三阶反幻方一共多少种。这是一个整数,不要填写任何多余内容。

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    // VB_king —— 2016_Finals_C_C++_2.cpp created by VB_KoKing on 2019-05-13:08.
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
     一个反幻方的镜像加旋转总共8次。
     Functions required by the program:
     
     Determination algorithm:
     
     Determining data structure:
     
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <set>
    
    using namespace std;
    
    int num[9],sum[8];
    
    void print(){
        for (int i = 0; i < 9; i++) {
            if (i%3==0) cout<<endl;
            cout<<num[i]<<' ';
        }
    }
    
    bool check(){
        memset(sum,0, sizeof(sum));
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++)
                sum[j]+=num[i+j*3];
            for (int j = 0; j < 3; j++)
                sum[j+3]+=num[3*i+j];
            sum[6]+=num[4*i];
            sum[7]+=num[2*(i+1)];
        }
        set <int> s;
        for (int i = 0; i < 8; i++)
            s.insert(sum[i]);
        if (s.size()==8) return true;
        return false;
    }
    
    int main(){
        int ans=0;
        for (int i = 1; i < 10; i++)
            num[i-1]=i;
        do {
            if (check()){
    //            print();
                ans++;
    //            cout<<endl;
            }
        }while (next_permutation(num,num+9));
        cout<<endl<<ans/8<<endl;
        return 0;
    }
    
  • 相关阅读:
    Spring bean相关
    Springboot消除switch-case方法
    Springcloud中的region和zone的使用
    SpringCloud-Eureka-服务注册是如何发起的
    SpringBoot-SpringCloud-版本对应关系
    SpringCloud-Eureka-Provider&Consumer
    激活IDEA
    搞懂spring事务
    部署spring Boot项目到tomcat
    springBoot项目打war包部署到tomcat上
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338307.html
Copyright © 2020-2023  润新知