最近查看了一些魔方阵的资料。网上有很多生成奇数阶魔方阵的方法, 也有一些生成双偶数阶魔方阵的方法。回去重点思考一下单偶数阶魔方阵的生成方法。今天终于编写调试(在Turboc 2.0中)成功,现在与大家分享。
有关魔方阵的资料,可参考https://www.bc-cn.net/bbs/dispbbs.asp?boardID=5&ID=28099
#include <stdio.h>
main()
{
int i,j,n,k,p,q,t,sum,a[20][20]={0};
printf("Please input N:\n");
scanf("%d",&n);
sum=n*(n*n+1)/2;
t=n;
if(n<3||n>20)
  printf("The number is out of range.");
else if(n%2!=0)    /* According to the De La Loubere's Method for odd level Rubik Cube */
       {
       k=1;
       i=0;
       j=(n-1)/2;
       while(k<=n*n)
         {
         while(a[i][j]==0)
           {
           a[i][j]=k;
           k++;
           if(i==0)
             i=n-1;
           else i=i-1;
           if(j==n-1)
             j=0;
           else j=j+1;
           }
         if(i==n-1)
           i=1;
         else i=i+2;
         if(j==0)
           j=n-1;
         else j=j-1;
         }
       }
    else   {
           if(n%4!=0)  /* According to the Yanghui's Extended Method for quadruple level Rubik Cube */
             n-=2;
           p=2;
           q=n*n+1;
           for(i=1;i<=n;)
             {
             while(i%4==0||i%4==1)
               {
               if(i%4==0)
                 {
                 p+=2;
                 q+=1;
                 }
               else q-=1;
               a[i][1]=q;
               q-=3;
               for(j=3;j<=n;j+=4)
                 {
                 a[i][j-1]=p;
                 a[i][j]=p+1;
                 p+=4;
                 }
               for(k=4;k<n;k+=4)
                 {
                 a[i][k]=q;
                 a[i][k+1]=q-1;
                 q-=4;
                 }         
               a[i][k]=q;
               i+=1;
               }
             while(i%4==2||i%4==3)
               {
               if(i%4==2)
                 {
                 p-=1;
                 q-=2;
                 }
               else p+=1;
               a[i][1]=p;
               p+=3;
               for(j=4;j<n;j+=4)
                 {
                 a[i][j]=p;
                 a[i][j+1]=p+1;
                 p+=4;
                 }
               for(k=3;k<=n;k+=4)
                 {
                 a[i][k-1]=q;
                 a[i][k]=q-1;
                 q-=4;
                 }
               a[i][j]=p;
               i+=1;
               }
             }
           if(t%4!=0)   /* According to the Expand Level Method for single even level Rubik Cube */
             {
             n+=2;
             for(i=1;i<=n;i++)
               for(j=1;j<=n;j++)
                 a[i][j]+=2*n-2;
             a[0][0]=n*n-n;
             a[n-1][n-1]=n+1;
             a[n-1][0]=n;
             a[0][n-1]=n*n-n+1;
             for(k=1;k<=(n/2-1);k++)
               {
               a[2*k-1][0]=2*k;
               a[2*k][0]=n*n-2*k;
               a[0][2*k-2]=n*n-n-2*k+2;
               a[0][2*k-1]=n+2*k;
               }
             a[1][0]=n*n-1;
             a[n/2][0]=1;
             a[n/2+1][0]=n/2+2;
             a[0][n-2]=n/2+1;
             for(k=1;k<n-1;k++)
               {
               a[n-1][k]=n*n+1-a[0][k];
               a[k][n-1]=n*n+1-a[k][0];
               }
             }
           }  
if(n>=3&&n<=20)
  {
  if(n%4!=0)
    {
    for(i=0;i<n;i++)
      {
      for(j=0;j<n;j++)
        printf("%3d ",a[i][j]);
      putchar('\n');
      }
    }
  else {
       for(i=1;i<=n;i++)
         {
         for(j=1;j<=n;j++)
           printf("%3d ",a[i][j]);
         putchar('\n');
         }
       }
  printf("The sum of the numbers in every row, column and diagonal is %d.\n",sum);
  }
}  
/* De La Loubere's Method: Place 1 in the first row, in the center of the column. Then go right and up one form, 
place N+1, and continue go, contunue place N+1, if the form you go has already had a number, return back, go 
down one form, place N+1, and contunue go right and up one form, place N+1, at last you will get odd level Rubik 
Cube.
* * 1 * *     * * 1 * *     * * 1 * *     * * 1 * *      *  *  1  8  *     17 24  1  8 15
* * * * *     * * * * *     * 5 * * *     * 5 * * *      *  5  7  *  *     23  5  7 14 16
* * * * *     * * * * *     4 * * * *     4 6 * * *      4  6  *  *  *      4  6 13 20 22
* * * * *     * * * * *     * * * * 3     * * * * 3     10  *  *  *  3     10 12 19 21  3
* * * * *     * * * 2 *     * * * 2 *     * * * 2 *     11  *  *  2  9     11 18 25  2  9 */
/* Yanghui's Extended Method: Divide the N*N phalanx into several 4*4 phalanxes. Mark the forms in every 4*4 
phalanx as below. Place i in the i's form of * form, place N*N-i in the i's form of @ form just as below.
@  *  *  @  @  *  *  @         01 02 03 04 05 06 07 08          64 02 03 61 60 06 07 57
*  @  @  *  *  @  @  *         09 10 11 12 13 14 15 16          09 55 54 12 13 51 50 16 
*  @  @  *  *  @  @  *         17 18 19 20 21 22 23 24          17 47 46 20 21 43 42 24  
@  *  *  @  @  *  *  @         25 26 27 28 29 30 31 32          40 26 27 37 36 30 31 33  
@  *  *  @  @  *  *  @         33 34 35 36 37 38 39 40          32 34 35 29 28 38 39 25 
*  @  @  *  *  @  @  *         41 42 43 44 45 46 47 48          41 23 22 44 45 19 18 48  
*  @  @  *  *  @  @  *         49 50 51 52 53 54 55 56          49 15 14 52 53 11 10 56  
@  *  *  @  @  *  *  @         57 58 59 60 61 62 63 64          08 58 59 05 04 62 63 01 */
/* Expand Level Method: Firstly construct a N-2 Rubic Cube which is a quadruple level Rubik Cube (the area 
of the *). Then add 2N-2 to all the numbers in the phalanx. Place N*N-N in the left top @ form, N+1 right bottom 
@ form, N left bottom @ form,N*N-N+1 right top @ form. Divide fisrt column(except two ends of @ form) into 2 
groups, - form is the first group, + form is another. Assign a[2k-1][0]=2k which is the first group, 
a[2k][0]=N*N-2k which is the second.(k=1,2...2/N-1) Make somme adjustment as follows: a[1][0]=N*N-1, [n/2][0]=1,
a[n/2+1][0]=N/2+1. Otherwise, the sum of the first column will not be equal to N*(N*N+1)/2. Divide first row
(except back end of @ form) into 2 groups, left top @ and - form is the first group,+ form is another. Assign 
a[0][2k-2]=N*N-N-2k+2 which is the first group, a[0][2k-1]=N+2k which is the second.(k=1,2...2/N-1)
Then # form can be caculated by the formula. For example, # form in the first row is equal to 2/N+1. 
@  +  -  +  #  @   @  +  -  +  # @   30  +  -  +  # 31   30 08 28 10  # 31   30 08 28 10 04 31
-  *  *  *  *  #   - 16 02 03 13 #    - 26 12 13 23  #   35 26 12 13 23  #   35 26 12 13 23 02 
+  *  *  *  *  #   + 05 11 10 08 #    + 15 21 20 18  #   34 15 21 20 18  #   34 15 21 20 18 03 
-  *  *  *  *  #   - 09 07 06 12 #    - 19 17 16 22  #   01 19 17 16 22  #   01 19 17 16 22 36
+  *  *  *  *  #   + 04 14 15 01 #    + 14 24 25 11  #   05 14 24 25 11  #   05 14 24 25 11 32
@  #  #  #  #  @   @  #  #  #  # @   06  #  #  #  # 07   06  #  #  #  #  #   06 29 09 27 33 07 */
  
  
[此贴子已经被作者于2005-11-25 13:05:50编辑过]



 
											





 
	    

 
	