/* Spring, 1997 AAMU Math and C.S. Department CMP507 Data Structure and Algorithms in C Dr. Peter Hor-Ching Wang 8 Qween problem */ /* -------------------------- global section ------------------------ */ #includeint column[8], /* queen */ row[8], /* position */ diag13[15], /* the same sum of i+j */ diag24[15]; /* the same difference of i - j + 7 */ FILE *fp; void initialize(); void process(); void show(); /* ---------------------------- main() ----------------------------- */ void main() { fp = fopen("8queen.out","w"); initialize(); process(0); fclose(fp); } /* ------------------------ show() -------------------------------- */ void show() { int n; for(n=0;n<8;n++) fprintf(fp,"q(%i)=%i, ",n+1,column[n]); fprintf(fp,"\n"); } /* ----------------------- process() ----------------------------- */ void process(i) int i; { int j; for(j=0;j<8;j++) { if(row[j] && diag13[i+j] && diag24[i-j+7]) { column[i] = j; row[j] = diag13[i+j] = diag24[i-j+7] = 0; if(i<7) process(i+1); else show(); row[j] = diag13[i+j] = diag24[i-j+7] = 1; } } } /* ---------------------------- initialize() ------------------------- */ void initialize() { int n; for(n=0;n<8;n++) column[n] = 0; for(n=0;n<8;n++) row[n] = 1; for(n=0;n<15;n++) { diag13[n] = diag24[n] = 1; } } /* -------------------------- The End ------------------------------- */