2015年第六届蓝桥杯C/C++ A组国赛 —— 第二题:四阶幻方

标题:四阶幻方

把1~16的数字填入4x4的方格中,使得行、列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方。

四阶幻方可能有很多方案。如果固定左上角为1,请计算一共有多少种方案。
比如:
1 2 15 16
12 14 3 5
13 7 10 4
8 11 6 9

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

就可以算为两种不同的方案。

请提交左上角固定为1时的所有方案数字,不要填写任何多余内容或说明文字。

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.
The world I see is all your shadow."

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;
}
原文地址:https://www.cnblogs.com/AlexKing007/p/12338248.html