home *** CD-ROM | disk | FTP | other *** search
- /************************************************
- * QUEEN1 - the first attempt at solving the *
- * queens problem. *
- * *
- * INP: c - the column # to place a queen in *
- * n - the maximum column (& row) number *
- * USES: Soln - updates this solution counter *
- * whenever a queen is successfully placed *
- * in the n'th column. *
- ************************************************/
-
- #define TRUE (1==1) /* Boolean true value */
- #define FALSE (1==0) /* Boolean false value */
- #define MAXBRD 25 /* Maximum board size */
-
- void PrintBoard(int);
- void queens(int c, int n)
- {
- extern int board[MAXBRD+1][MAXBRD+1];
- extern long Solns;
- int i,j,legal;
-
- /* Have we found a solution? */
- if (c==n)
- {
- ++Solns;
- return;
- }
-
- /* Check each spot in the column */
- for (i=0; i<n; i++)
- {
- legal=TRUE; /* Legal until disproven*/
- for (j=1; j<=c; j++)
- {
- /* Check row */
- if (board[i][c-j] != 0)
- legal = FALSE;
-
- /* Check diagonal 1 */
- if (i+j<n)
- if (board[i+j][c-j] != 0)
- legal = FALSE;
-
- /* Check diagonal 2 */
- if (i-j>=0)
- if (board[i-j][c-j] != 0)
- legal = FALSE;
- }
- if (legal)
- {
- board[i][c]=1; /* Put queen here */
- queens(c+1,n); /* Process next col */
- board[i][c]=0; /* Remove & try next*/
- }
- }
- }
-