|
楼主 |
发表于 2003-7-12 03:59:01
|
显示全部楼层
exit 函数在哪里??
我的n阶完美幻方程序写好了。
- #include<time.h>
- #include<stdio.h>
- #define Max 20
- #define MAX(x,y) (x)>(y)?(x) : (y)
- int count=0;
- int END;
- int M,MM;
- int sum;
- int maxnum,minnum;
- int data[Max*Max];
- int flags[Max*Max];
- FILE *outfile;
- time_t start,end;
- int min_not_used[Max+1]={0};
- int max_not_used[Max+1]={0};
- class STACKDATA{
- private:
- int buff[Max+1];
- int cursor;
- public:
- STACKDATA(){
- cursor=-1;
- sum=0;
- }
- ~STACKDATA(){
- }
- int push(int newdata){
- buff[++cursor]=newdata;
- sum+=newdata;
- return 1;
- }
- int pop(){
- if(cursor<0)return -1;
- sum-=buff[cursor];
- return buff[cursor--];
- }
- int sum;
- };
- void init();
- void bye();
- void range(int,int,int,int );
- void magic(int);
- void output(void);
- STACKDATA rowbuff[Max+1];
- STACKDATA colbuff[Max+1];
- STACKDATA ldbuff[Max+1];
- STACKDATA rdbuff[Max+1];
- /*********** bigin main *************/
- int main(int argc,char *argv[]) {
- if(argc!=2){
- fprintf(stderr,"usage: %s num \n",argv[0]);
- return 1;
- }
- sscanf(argv[1],"%d",&M);
- init();
- magic(0);
- bye();
- return 0;
- }
- /****** end of main *****************/
- void init()
- {
-
- int i;
- outfile=fopen("result.txt","w");
- for(i=0;i<Max*Max;i++)flags[i]=0;
- if(M>Max){puts("Max M exceed!!\n");}
- MM=M*M;
- sum=(MM+1)*M/2;
- END=MM-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));
- fprintf(outfile,"%d matchs found!",count);
- fclose(outfile);
- }
- /***************************************************/
- void magic(int n)
- {
- int i,ii;
- int max;
- int col,row,diag;
- row=n/M;
- col=n%M;
- diag=(col-row+M)%M;
- range(n,row,col,diag);
- for(i=minnum,max=maxnum;i<=max;i++) {
- if(flags[i]==0) {
- ii=i+1;
- flags[i]=1;
- data[n]=ii;
- rowbuff[row].push(ii);
- colbuff[col].push(ii);
- ldbuff[diag].push(ii);
- rdbuff[diag].push(ii);
- if(n==(END)){
- output();
- }
- else magic(n+1);
- flags[i]=0;
- rowbuff[row].pop();
- colbuff[col].pop();
- ldbuff[diag].pop();
- rdbuff[diag].pop();
- }
- }
- return;
- }
- /**********************************************************/
- void output()
- {
- int i,j;
-
- count++;
- fprintf(outfile,"%d:\n",count);
- for(i=0;i<M;i++){
- for(j=0;j<M;j++){
- fprintf(outfile,"%5d",data[i*M+j]);
- }
- fputc('\n',outfile);
- }
- fflush(outfile);
- }
- /******************* This function to large!!! ************/
- void range(int n,int row,int col,int diag) {
- static int used;
- static int bottom,top,left;
- static int c;
- static int tmp;
- static int i,j,k;
- k=MAX(M-col,M-row);
- if(k>M)k=M;
- for(i=0,c=1,tmp=0;c<=k;i++){
- if(flags[i]==0){
- tmp+=i+1;
- c++;
- min_not_used[c]=tmp;
- }
- }
- for(i=END,c=1,tmp=0;c<=k;i--){
- if(flags[i]==0){
- tmp+=i+1;
- c++;
- max_not_used[c]=tmp;
- }
- }
- used=rowbuff[row].sum;
- left=sum-used-1;
- bottom=max_not_used[M-col];
- top =min_not_used[M-col];
- minnum=left-bottom;
- maxnum=left-top;
-
- used=colbuff[col].sum;
- left=sum-used-1;
- bottom=max_not_used[M-row];
- top =min_not_used[M-row];
- minnum=minnum<left-bottom?left-bottom:minnum;
- maxnum=maxnum>left-top?left-top:maxnum;
- used=ldbuff[diag].sum;
- left=sum-used-1;
- bottom=max_not_used[M-row];
- top =min_not_used[M-row];
- minnum=minnum<left-bottom?left-bottom:minnum;
- maxnum=maxnum>left-top?left-top:maxnum;
- used=rdbuff[diag].sum;
- left=sum-used-1;
- bottom=max_not_used[M-row];
- top =min_not_used[M-row];
- minnum=minnum<left-bottom?left-bottom:minnum;
- maxnum=maxnum>left-top?left-top:maxnum;
- if(minnum<0)minnum=0;
- if(maxnum>=MM)maxnum=MM-1;
- }
- /***************** end of program *****************************************/
复制代码 |
|