# The sixth Blue Bridge Cup group a C / C + + national competition in 2015

## Title: magic square of fourth order

Fill the numbers from 1 to 16 into the 4x4 square, so that the sum of rows, columns and two diagonals are equal, which is called the fourth-order magic square when it meets such characteristics.

There may be many schemes for the fourth order magic square. If the fixed upper left corner is 1, please calculate the total number of options.
For example:
1 2 15 16
12 14 3 5
13 7 10 4
8 11 6 9

And:
1 12 13 8
2 14 7 11
15 3 10 6
16 5 4 9

It can be regarded as two different schemes.

Please submit all scheme numbers when the upper left corner is fixed to 1, and do not fill in any redundant content or explanatory text.

## 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.     .^
*/

/* Procedural objectives:

Variables required by the program:

Procedural thinking:

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.

FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
#include <algorithm>

using namespace std;

int num[17];
int sum[11];
int ans[17];
bool vis[17];
long long temp=0;

void dfs(int index){
if(index==5){
sum[1]=ans[1]+ans[2]+ans[3]+ans[4];
}
if(index==9){
sum[2]=ans[5]+ans[6]+ans[7]+ans[8];
if(sum[2]!=sum[1])
return;
}
if(index==13){
sum[3]=ans[9]+ans[10]+ans[11]+ans[12];
if(sum[3]!=sum[1])
return;
}
if(index==14){
sum[5]=ans[1]+ans[5]+ans[9]+ans[13];
sum[10]=ans[4]+ans[7]+ans[10]+ans[13];
if(sum[5]!=sum[1] || sum[10]!=sum[1])
return;
}
if(index==15){
sum[6]=ans[2]+ans[6]+ans[10]+ans[14];
if(sum[6]!=sum[1])
return;
}
if(index==16){
sum[7]=ans[3]+ans[7]+ans[11]+ans[15];
if(sum[7]!=sum[1])
return;
}
if(index==17){
sum[4]=ans[13]+ans[14]+ans[15]+ans[16];
sum[8]=ans[4]+ans[8]+ans[12]+ans[16];
sum[9]=ans[1]+ans[6]+ans[11]+ans[16];
if(sum[4]!=sum[1] || sum[8]!=sum[1] || sum[9]!=sum[1])
return;
for(int i=1;i<17;i++){
cout<<ans[i]<<' ';
if(i%4==0)
cout<<endl;
}
for(int i=1;i<11;i++){
cout<<"sum["<<i<<"]="<<sum[i]<<' ';
}
cout<<endl<<endl;
temp++;
return;
}
for(int i=1;i<17;i++){
if(!vis[i]){
vis[i]=true;
ans[index]=i;
dfs(index+1);
vis[i]=false;
}
}
}

int main(){
for(int i=1;i<17;i++){
num[i]=i;
vis[i]=false;
}
ans[1]=1;vis[1]=true;
dfs(2);
cout<<temp<<endl;
return 0;
}
```

Posted on Wed, 06 Nov 2019 11:06:54 -0500 by Wave