|
N 阶方阵是 把 1 - N^2 填在 N*N 的矩阵里 ,
使得 每行、每列、对角线的和都相等;
N 能做到多大就多大拉。
下面是我的一个程序(N=5 时就难于忍受了):
- /***********************
- *** Fifth version *****
- ***********************/
- #include<time.h>
- #include<stdio.h>
- #define Max 100
- int END;
- int M;
- int sum;
- int maxnum,minnum;
- int data[Max*Max];
- int flags[Max*Max];
- FILE *outfile;
- time_t start,end;
- int bottom,top,left;
- int head,tail,cursor;
- void init();
- void bye();
- void range(int);
- void magic(int);
- void output(void);
- void print_status(void);
- main()
- {
- init();
- magic(0);
- bye();
- return 0;
- }
- void init()
- {
-
- int i;
-
- outfile=fopen("result.txt","w");
-
- for(i=0;i<Max*Max;i++)flags[i]=0;
- printf(" Input M:");
- scanf("%d",&M);
- sum=(M*M+1)*M/2;
- END=M*M-1;
- start=time(&start);
- fprintf(outfile,"start at %s\n//////////////////////////////////\n",ctime(&start));
- }
- void bye()
- {
- end=time(&end);
- fprintf(outfile,"\n//////////////////////////////////\nend at %s\n",ctime(&end));
- fclose(outfile);
-
- }
- void magic(int n)
- {
- int i;
- int max;
-
- range(n);
- i=minnum;
- max=maxnum;
- for(;i<=max;i++)
- {if(flags[i]==0){
- flags[i]=1;
- data[n]=i+1;
- if(n==(END)){output(); }
- else magic(n+1);
- flags[i]=0;
- }
- }
- return;
- }
- void output()
- {
- int i,j;
-
- for(i=0;i<M;i++)
- {for(j=0;j<M;j++)
- {
- fprintf(outfile,"%5d",data[i*M+j]);
- }
- fputc('\n',outfile);
- }
- fputs("*****************************************\n",outfile);
- fflush(outfile);
- }
- /******************* This function to large!!! ************/
- /* calculate the range for the next data */
- void range(int n) //not so easy to understand.
- {
- static int i,j,k;
- static int used=0;
- used=0;
- i=(n/M)*M;
- cursor=n%M;
- j=i+cursor;
- left=sum;
- for(;i<j;i++)
- used+=data[i];
- k=M-cursor-1;
- top=bottom=sum-used-1;
- for(i=END;k>0;i--)
- if(flags[i]==0)
- {bottom-=i+1;k--;}
- tail=END;
- k=M-cursor-1;
- for(i=0;k>0;i++)
- if(flags[i]==0)
- {top-=i+1;k--;}
- head=0;
- for(;flags[head]!=0;head++);
-
- minnum=bottom>head?bottom:head;
-
- for(;flags[tail]!=0;tail--);
- maxnum=top<tail?top:tail;
- cursor=n/M;
- j=(n%M);
- for(used=0,i=0;i<cursor;i++)
- used+=data[i*M+j];
- top=bottom=sum-used-1;
- for(i=END,k=M-cursor-1;k>0;i--)
- if(flags[i]==0)
- {bottom-=i+1;k--;}
- for(i=0,k=M-cursor-1;k>0;i++)
- if(flags[i]==0)
- {top-=i+1;k--;}
- minnum=minnum>bottom?minnum:bottom;
- maxnum=maxnum<top?maxnum:top;
- if(cursor==j)
- {
-
- for(used=0,i=0;i<cursor;i++)
- used+=data[i*M+i];
- top=bottom=sum-used-1;
-
- for(i=END,k=M-cursor-1;k>0;i--)
- if(flags[i]==0)
- {bottom-=i+1;k--;}
-
- for(i=0,k=M-cursor-1;k>0;i++)
- if(flags[i]==0)
- {top-=i+1;k--;}
- minnum=minnum>bottom?minnum:bottom;
- maxnum=maxnum<top?maxnum:top;
- }
- if((cursor+j+1)==M)
- {
- for(used=0,i=0,j=M-1;i<cursor;i++,j--)
- used+=data[i*M+j];
- top=bottom=sum-used-1;
-
- for(i=END,k=M-cursor-1;k>0;i--)
- if(flags[i]==0)
- {bottom-=i+1;k--;}
-
- for(i=0,k=M-cursor-1;k>0;i++)
- if(flags[i]==0)
- {top-=i+1;k--;}
- minnum=minnum>bottom?minnum:bottom;
- maxnum=maxnum<top?maxnum:top;
- }
- }
复制代码 |
|