八皇后问题

这个问题是耳熟能详了,不再赘述。

先看几种不同的实现。

1. 

#include <sys/time.h>
#include <stdio.h>

#define MAX_SIZE	50

static int n = 0;
static int tot = 0;
static int C[MAX_SIZE];

void search(int cur)
{
	if(cur == n)
	{
		tot++;
	}
	else
	{
		int i;
		for(i = 0; i < n; i++)
		{
			int ok = 1;
			C[cur] = i;
			
			int j;
			for(j = 0; j < cur; j++)
			{
				if( C[cur] == C[j] || cur - C[cur] == j - C[j] || 
					C[cur] + cur == C[j] + j)
				{
					ok = 0;
					break;
				}
			}

			if(ok)
			{
				search(cur + 1);
			}
		}
	}
}


int main(int argc, char **argv)
{
	n = 16;
	struct timeval start, end;
	gettimeofday(&start, NULL);
	search(0);
	gettimeofday(&end, NULL);
	printf("time = %f\n", (end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1000000.0));
	printf("%d\n", tot);
	return 0;
}

这个实现是把“皇后”逐行排在不同的列上,在判断下一个位置的时候,只需要判断列和对角线是否合适了。

使用一个一维数组,把列号保存在里面,用行号来索引。


2.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

long sum = 0, upperlim = 1;

void test(long row, long ld, long rd) 
{

   if (row != upperlim)
   {   
      long pos = upperlim & ~(row | ld | rd);
      while (pos)
      {
         long p = pos & -pos;

         pos -= p;
         test(row + p, (ld + p) << 1, (rd + p) >> 1); 
      }
   } 
   else
      sum++;
}

int main(int argc, char *argv[])
{
   time_t tm; 
   int n = 16; 

   if (argc != 1)
   n = atoi(argv[1]);
   tm = time(0);
   if ((n < 1) || (n > 32))
   {   
      exit(-1);
   }   
   printf("%d-queens\n", n); 
   upperlim = (upperlim << n) - 1;

   test(0, 0, 0); 
   printf("there are %ld combinations, time spent: %d secnods \n", sum, (int) (time(0) - tm));
}
里面变量的含义:

ld, rd: 值的二进制位数为1 表示从右边数对应列不能放置皇后

变量 pos的二进制表达式里面的1表示可以放置皇后的列。

pos & -pos 这个表达式其实是把pos二进制值除了最右边的第一个1之外,所有的1都设置成为0.其实,就是右数第一个可以放置皇后的位置。

比如, 假如 pos = 65520 = 0xfff0 = 1111111111110000

则 -pos = 11111111111111110000000000010000

pos & -pos = 10000


3.

#ifdef WIN32

#define MAX_BOARDSIZE 21
typedef unsigned __int64 SOLUTIONTYPE;

#else

#define MAX_BOARDSIZE 18
typedef unsigned long SOLUTIONTYPE;

#endif

#define MIN_BOARDSIZE 2

SOLUTIONTYPE g_numsolutions = 0; 

/* The function which calculates the N queen solutions.
   We calculate one-half the solutions, then flip the results over
   the "Y axis" of the board.  Every solution can be reflected that
   way to generate another unique solution (assuming the board size
   isn't 1 x 1).  That's because a solution cannot be symmetrical
   across the Y-axis (because you can't have two queens in the same
   horizontal row).  A solution also cannot consist of queens 
   down the middle column of a board with an odd number of columns,
   since you can't have two queens in the same vertical row.

   This is a backtracking algorithm.  We place a queen in the top
   row, then note the column and diagonals it occupies.  We then 
   place a queen in the next row down, taking care not to place it
   in the same column or diagonal.  We then update the occupied
   columns & diagonals & move on to the next row.  If no position
   is open in the next row, we back track to the previous row & move
   the queen over to the next available spot in its row & the process
   starts over again.
*/
void Nqueen(int board_size)
{
    int aQueenBitRes[MAX_BOARDSIZE]; /* results */
    int aQueenBitCol[MAX_BOARDSIZE]; /* marks colummns which already have queens */
    int aQueenBitPosDiag[MAX_BOARDSIZE]; /* marks "positive diagonals" which already have queens */
    int aQueenBitNegDiag[MAX_BOARDSIZE]; /* marks "negative diagonals" which already have queens */
    int aStack[MAX_BOARDSIZE + 2]; /* we use a stack instead of recursion */
    register int* pnStack;


    register int numrows = 0; /* numrows redundant - could use stack */
    register unsigned int lsb; /* least significant bit */
    register unsigned int bitfield; /* bits which are set mark possible positions for a queen */
    int i;
    int odd = board_size & 1; /* 0 if board_size even, 1 if odd */
    int board_minus = board_size - 1; /* board size - 1 */
    int mask = (1 << board_size) - 1; /* if board size is N, mask consists of N 1's */


    /* Initialize stack */
    aStack[0] = -1; /* set sentinel -- signifies end of stack */

    /* NOTE: (board_size & 1) is true iff board_size is odd */
    /* We need to loop through 2x if board_size is odd */
    for (i = 0; i < (1 + odd); ++i)
    {
        /* We don't have to optimize this part; it ain't the 
           critical loop */
        bitfield = 0;
        if (0 == i)
        {
            /* Handle half of the board, except the middle
               column. So if the board is 5 x 5, the first
               row will be: 00011, since we're not worrying
               about placing a queen in the center column (yet).
            */
            int half = board_size>>1; /* divide by two */
            /* fill in rightmost 1's in bitfield for half of board_size
               If board_size is 7, half of that is 3 (we're discarding the remainder)
               and bitfield will be set to 111 in binary. */
            bitfield = (1 << half) - 1;
            pnStack = aStack + 1; /* stack pointer */
    
            aQueenBitRes[0] = 0;
            aQueenBitCol[0] = aQueenBitPosDiag[0] = aQueenBitNegDiag[0] = 0;
        }
        else
        {
            /* Handle the middle column (of a odd-sized board).
               Set middle column bit to 1, then set
               half of next row.
               So we're processing first row (one element) & half of next.
               So if the board is 5 x 5, the first row will be: 00100, and
               the next row will be 00011.
            */
            bitfield = 1 << (board_size >> 1);
            numrows = 1; /* prob. already 0 */

            /* The first row just has one queen (in the middle column).*/
            aQueenBitRes[0] = bitfield;
            aQueenBitCol[0] = aQueenBitPosDiag[0] = aQueenBitNegDiag[0] = 0;
            aQueenBitCol[1] = bitfield;

            /* Now do the next row.  Only set bits in half of it, because we'll
               flip the results over the "Y-axis".  */
            aQueenBitNegDiag[1] = (bitfield >> 1);
            aQueenBitPosDiag[1] = (bitfield << 1);
            pnStack = aStack + 1; /* stack pointer */
            *pnStack++ = 0; /* we're done w/ this row -- only 1 element & we've done it */
            bitfield = (bitfield - 1) >> 1; /* bitfield -1 is all 1's to the left of the single 1 */
        }

        /* this is the critical loop */
        for (;;)
        {
            /* could use 
               lsb = bitfield ^ (bitfield & (bitfield -1)); 
               to get first (least sig) "1" bit, but that's slower. */
            lsb = -((signed)bitfield) & bitfield; /* this assumes a 2's complement architecture */
            if (0 == bitfield) 
            {
                bitfield = *--pnStack; /* get prev. bitfield from stack */
                if (pnStack == aStack) { /* if sentinel hit.... */
                    break ;
                }
                --numrows;
                continue;
            }
            bitfield &= ~lsb; /* toggle off this bit so we don't try it again */

            aQueenBitRes[numrows] = lsb; /* save the result */
            if (numrows < board_minus) /* we still have more rows to process? */
            {
                int n = numrows++;
                aQueenBitCol[numrows] = aQueenBitCol[n] | lsb;
                aQueenBitNegDiag[numrows] = (aQueenBitNegDiag[n] | lsb) >> 1;
                aQueenBitPosDiag[numrows] = (aQueenBitPosDiag[n] | lsb) << 1;
                *pnStack++ = bitfield;
                /* We can't consider positions for the queen which are in the same
                   column, same positive diagonal, or same negative diagonal as another
                   queen already on the board. */
                bitfield = mask & ~(aQueenBitCol[numrows] | aQueenBitNegDiag[numrows] | aQueenBitPosDiag[numrows]);
                continue;
            }
            else
            {
                /* We have no more rows to process; we found a solution. */
                /* Comment out the call to printtable in order to print the solutions as board position*/
                /* printtable(board_size, aQueenBitRes, g_numsolutions + 1);  */
                ++g_numsolutions;
                bitfield = *--pnStack;
                --numrows;
                continue;
            }
        }
    }

    /* multiply solutions by two, to count mirror images */
    g_numsolutions *= 2;
}

参考:

1. http://www.jsomers.com/nqueen_demo/nqueens.html

2. http://blog.csdn.net/justme0/article/details/7540425

你可能感兴趣的:(八皇后问题)