/***************** * qN.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: * brute force, with recursion. 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. * * Also find 8 rotations and reflections, and check to see * how many of the 8 look like each solution. Use this * to calculate how many unique solutions for each N, too. * * This "qN" version I used to run some other numbers besides the * chessboard N=8. * * Jim Mahoney 10/19/98 * * history * * - added elapsed time 2/7/03 * *****************************/ #include #include /* =========================================================== */ /* ====== globals =========================================== */ /* ========================================================== */ /******************* ** These set the range of N's to look at. ** Set them to ******/ int firstN = 2; int lastN = 16; /* MAKE SURE Nsize IS AT LEAST THIS BIG */ /* The board will be N x N */ #define Nsize 24 int N; /* 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 0 /* 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[Nsize][Nsize]; int board_print[Nsize][Nsize]; /* only queen postions, not "seeing" parts */ /* storage for symmetry calculations */ int symm[Nsize][Nsize][NumSymmetries]; #define empty 0 /* Number of distinct solutions found */ int solution_count = 0; /* Number of unique solutions found */ float unique_count = 0.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 board_print */ int IsEqual(int sy){ int r,c; for (r=0;r=0)&&(c=0)&&(c