/************************************************************************//* after eight questions *//************************************************************************/#include <stdio.h>int count = 0;//judge whether the current position can put Queen Int iscorrect (Int i, int j, int (*Q) [8]) {int s,t;//determines if Queen for (s=i,t=0;t<8;t++) if can be placed on a line (q[s][t]==1 && &NBSP;T!=J) return 0;//Determine if a column can put Queen for (t=j,s=0;s<8;s++) if (q[s][t]==1 && s!=i) return 0;//judge if the left can put Queen for (S=i-1,t=j-1;s>=0&&t>=0;s--, t--) if (q[s][t]==1) return 0;// Judge if the right can put queen for (s=i+1,t=j+1;s<8&&t<8;s++,t++) if (q[s][t]==1) return 0;//determine if the Queen for (S=i-1,t=j+1;s>=0&&t<8;s--, t++) can be placed on the right (q[s][t]==1) return 0;//determine if the lower left can put Queen for (s=i+1,t=j-1;s<8&&t>=0;s++,t--) if (q[s][t]==1) return 0;//other conditions return 1;} Place Queen Void queen (int j, int (*Q) [8]) {int i,k;if (j==8)//If all 8 queens are completely placed {for (i=0;i<8;i+ +) {for (k=0;k<8;k++) printf ("%d ", Q[i][k]);p rintf ("\ n");} printf ("\ n"); Count++;return;} for (i=0;i<8;i++) if (Iscorrect (i,j,q)) {q[i][j]=1; Queen (J+1,Q); q[i][j]=0;}} Int main () {int q[8][8];int i,j;for (i=0;i<8;i++) for (j=0;j<8;j++) q[i][j]=0; Queen (0,q);p rintf ("%d\n", count); return 0;}
Classical backtracking algorithm--eight problems