用回溯法解决八皇后问题的C语言程序

#include #include int col[9]={0},a[9]; int b[17],c[17]; main() { int m,good; int i,j,k; char q; for(i=0; i<17; i++) { if(i<9) a[i]=1; b[i]=1; c[i]=1; } good=1; col[1]=1; m=1; while(col[0]!=1) { if(good) if(m==8) { for(i=1; i<9; i++) printf("col[%d] %d\n",i,col[i]); printf("input 'q' to quit\n"); scanf("%c",&q); getchar(); if(q=='q'||q=='Q') exit(0); while(col[m]==8) { m--; a[col[m]]=b[m+col[m]]=c[8+m-col[m]]=1; } a[col[m]]=b[m+col[m]]=c[8+m-col[m]]=1; col[m]++; } else { a[col[m]]=b[m+col[m]]=c[8+m-col[m]]=0; m++; col[m]=1; } else { while(col[m]==8) { m--; a[col[m]]=b[m+col[m]]=c[8+m-col[m]]=1; } col[m]++; } good=a[col[m]]&&b[m+col[m]]&&c[8+m-col[m]]; } }

    推荐阅读