幻方算法(Magic Square)学习笔记

来源:互联网 发布:msf漏洞渗通135端口 编辑:程序博客网 时间:2024/06/05 01:54
幻方算法(Magic Square)学习笔记

一、幻方按照阶数可成了三类,奇数阶幻方双偶阶幻方单偶阶幻方

二、奇数阶幻方(劳伯法)

奇数阶幻方最经典的填法是罗伯法。填写的方法是:

1(或最小的数)放在第一行正中;按以下规律排列剩下的(n×n1)个数:
1每一个数放在前一个数的右上一格;

(2如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列;

3如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行;

4如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在底行且最左列;

5如果这个数所要放的格已经有数填入,那么就把它放在前一个数的下一行同一列的格内。

例,用该填法获得的5阶幻方:

17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

二、双偶数阶幻方(海尔法)

所谓双偶阶幻方就是当n可以被4整除时的偶阶幻方,即4K阶幻方。在说解法之前我们先说明一个“互补数”定义:就是在n阶幻方中,如果两个数的和等于幻方中最大的数与1的和(即n×n1),我们称它们为一对互补数。如在三阶幻方中,每一对和为10的数,是一对互补数 ;在四阶幻方中,每一对和为17的数,是一对互补数。

双偶数阶幻方最经典的填法是海尔法。填写的方法是:

8阶幻方为例:
1先把数字按顺序填。然后,按4×4把它分割成4块(如图)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

2每个小方阵对角线上的数字(如左上角小方阵部分),换成和它互补的数。

64

2

3

61

60

6

7

57

9

55

54

12

13

51

50

16

17

47

46

20

21

43

42

24

40

26

27

37

36

30

31

33

32

34

35

29

28

38

39

25

41

23

22

44

45

19

18

48

49

15

14

52

53

11

10

56

8

58

59

5

4

62

63

1



三、单偶数阶幻方(斯特拉兹法)

所谓单偶阶幻方就是当n不可以被4整除时的偶阶幻方,即4K+2阶幻方。如(n=61014……)的幻方。

单偶数阶幻方最经典的填法是斯特拉兹法。填写的方法是:

10阶幻方为例。这时,k=2
1)把魔方阵分为ABCD四个象限,这样每一个象限肯定是奇数阶。用罗伯法,依次在A象限,D象限,B象限,C象限按奇数阶幻方的填法填数。


2)在A象限的中间行、中间格开始,按自左向右的方向,标出k格。A象限的其它行则标出最左边的k格。将这些格,和C象限相对位置上的数互换位置。


3)在B象限所有行的中间格,自右向左,标出k1格。(注:6阶幻方由于k1=0,所以不用再作BD象限的数据交换),将这些格,和D象限相对位置上的数互换位置。

四、源代码如下,已加详细注释

[cpp] view plaincopyprint?
#include<stdio.h>   #include<stdlib.h>     int array[15][15];    int init(int degree)                                  //初始化   {      int i;      int j;            for(i=0; i<=degree+1; i++)      for(j=0; j<=degree+1; j++)          array[i][j] = 0;      return 0;  }    int test_print(int x, int y, int w, int h)            //测试用的,输出以(x,y)为原点,宽为w,高为h,这个区域的数值   {      int i;      int j;      for(i=y; i<=y+h-1; i++){          for(j=x; j<=x+w-1; j++){              printf("%2d ",array[i][j]);          }          printf("\n");      }      return 0;  }    int lao_bo_er(int degree, int x, int y, int num)      //劳伯法   {      int i;      int j;      int k;            i = y;      j = degree/2 + x;      for(k=num; k<=num+degree*degree-1; k++){          array[i][j] = k;          if((k-num+1)%degree == 0){            //如果这个数所要放的格已经有数填入               i = (i-y+1)%degree+y;          }          else{                                 //每一个数放在前一个数的右上一格               i = (i-y-1+degree)%degree+y;              j = (j-x+1)%degree+x;          }      }      return 0;  }    int seq_range(int degree)                             //把数字按顺序填   {      int i;      int j;      int num;            num = 1;      for(i=1; i<=degree; i++){          for(j=1; j<=degree; j++){              array[i][j] = num++;          }      }      return 0;  }    int si_te_la_zi(int degree, int x, int y, int num)    //斯特拉兹法   {      int deg;      int k;      int temp;      int i;      int j;            deg = degree/2;      lao_bo_er(deg, x, y, num);                    //用罗伯法,依次在A象限,D象限,B象限,C象限按奇数阶幻方的填法填数       lao_bo_er(deg, x+deg, y, num+2*deg*deg);      lao_bo_er(deg, x, y+deg, num+3*deg*deg);      lao_bo_er(deg, x+deg, y+deg, num+deg*deg);        k = (degree-2)/4;      for(i=1; i<=deg; i++){                        //A象限和C象限对换数据           for(j=1; j<=k; j++){              temp = array[i][j];              array[i][j] = array[i+deg][j];              array[i+deg][j]=temp;          }          for(j=deg+deg/2+1; j>=deg+deg/2-k+3; j--){              temp = array[i][j];              array[i][j] = array[i+deg][j];              array[i+deg][j]=temp;          }      }            for(i=j=1; j<=deg/2+k; j++){                  //B象限和D象限对换数据           temp = array[i+deg/2][j];          array[i+deg/2][j] = array[i+deg+deg/2][j];          array[i+deg+deg/2][j]=temp;      }            return 0;  }    int hai_er_fa(int degree)                             //海尔法   {      int i;      int j;      int complement;      int deg;            seq_range(degree);            complement = degree*degree+1;      deg = degree/4;      for(i=0; i<deg; i++){          for(j=0; j<deg; j++){                 //对角线上的数字换成和它互补的数               array[i*4+1][j*4+1] = complement - array[i*4+1][j*4+1];              array[i*4+1][j*4+4] = complement - array[i*4+1][j*4+4];              array[i*4+4][j*4+1] = complement - array[i*4+4][j*4+1];              array[i*4+4][j*4+4] = complement - array[i*4+4][j*4+4];                            array[i*4+2][j*4+2] = complement - array[i*4+2][j*4+2];              array[i*4+2][j*4+3] = complement - array[i*4+2][j*4+3];              array[i*4+3][j*4+2] = complement - array[i*4+3][j*4+2];              array[i*4+3][j*4+3] = complement - array[i*4+3][j*4+3];          }      }      return 0;  }    int main()  {      int degree;      printf("please input the degree\n");      scanf("%d",°ree);      init(degree);      if(degree%2 == 1){                            //奇数阶幻方           lao_bo_er(degree,1,1,1);          test_print(1,1,degree,degree);      }      else if(degree%4 == 2){                       //双偶阶幻方           si_te_la_zi(degree, 1, 1, 1);          test_print(1,1,degree,degree);      }      else{                                         //单偶阶幻方           hai_er_fa(degree);          test_print(1,1,degree,degree);      }            return 0;  

原创粉丝点击