/***************** * q8.c * * will solve the "eight queens" problem, * which is to place eight queens on a chess * board such that no two can see each other * along any row, column, or diagonal. * * Approach 1: * brute force. Place one queen per row, * marking all spots she can "see" as unavailable. * Check all possible combinations, counting * (and optionally printing out) all that work. * * Jim Mahoney 10/19/98 *****************************/ #include /* =========================================================== */ /* ====== globals =========================================== */ /* ========================================================== */ /* The board will be N x N */ #define N 8 /* Save up to this many solutions */ #define MaxSolns 200 /* number of possible symmetries of a square pattern */ #define NumSymmetries 8 /* 1 => yes, print each position as we go; 0=> no, just count */ /* and store 'em. */ #define print_flag 0 /* same; 0 or 1 depending on whether symmetry stuff should be printed. */ #define PrintAnalysis 1 /* global "chess board". */ /* Each entry is 0 for empty; >0 for queen, <0 for "can see it" */ /* We keep track of which queen "sees" each square so that we */ /* can back up easily, column by column. /* Indices are [row][column] */ int board[N][N]; int board_print[N][N]; /* only queen postions, not "seeing" parts */ int solutions[N][N][MaxSolns]; /* saved board_prints */ int symmetries[MaxSolns]; /* record symmetry of each sol'n */ int IsThisAVariation[MaxSolns]; /* 0 if 1st of its kind; 1 if seen */ int symm[N][N][NumSymmetries]; /* storage for symmetry calculations */ #define empty 0 /* Number of distinct solutions found */ int solution_count = 0; /* Number of unique solutions found */ int unique_count = 0; /* =========================================================== */ /* ====== code ================================================= */ /* =========================================================== */ /* ------------------------------------------------------------ */ /* sign(int j) returns -1,0,1 depending on the sign of j. */ int sign(int j){ if (j>0) return(1); else if (j==0) return(0); else return(-1); } /* ------------------------------------------------------------ */ /* check to see if Symm board is same as one of of solutions */ int IsEqual(int sy, int so){ int r,c; for (r=0;r=0)&&(c=0)&&(c