Eight Queens problem (c #)

All things the old-fashioned. Just finishing writing this under their own ideas. I do not know the eight queens problem, see Baidu library

Analysis 1:

  What is horizontal, vertical, diagonal not conflict? The first two are easy to understand. Diagonal it? It is translated into a mathematical line (two-line) position where any two of the formed Queen slope can not be 1 or -1;

  How to traverse the entire board? The simplest is eight heavy loop to walk through each line. Then diverted through each column in the heavy cycle.

  Analog place: traversing the 0th row, first place (0,0) position determines the security;

        Traversing the first row, it is placed (1,0), insecure. Placement (1,1), or unsafe. Placing (1,2), security;

        Traversing the second line. . . . .

   What is safe, unsafe? Determining whether security needs will be evaluated and Queen have been previously placed. Since it is the Queen has been placed, then you must select the data structure to store the Queen. A two-dimensional array or what? Since each line can only place a queen, as long as we know in a row, a column placed the queen on OK. Corresponding to the integer array int [] is sufficient, (i.e. the subscript indicates the line, the value of the columns. The Queen [row] = col, row indicates the row col column placed queen.). When a row, b columns placed a queen, if found to meet the following conditions. They are considered unsafe.

  1. counterparts can not, because the data structure can be employed Int [] is not a prerequisite for the peer. Eliminating the need for judgment.

  2 can not be the same column, Queen any col [row] = col in = b;

  3 can not be inclined, Queen [row] = col any (col-b) / (row-a) = 1 or (col-b) / (row-a) = - 1;

    code show as below:

        /// <Summary> /// determines whether a column in a row safety. /// </ Summary> /// <param name = "A"> line </ param> /// <param name = "B"> column </ param> /// <Returns> </ Returns> public BOOL IsSafe ( int a, int B)         { for ( int row = 0 ; row <a; row ++) // traverse the front of the line, is not put back. Not need to traverse.             { IF (B == Queen [Row]) // same column                 { return to false ; 













(b - Queen[row] == a - row || b - Queen[row] == row - a)//斜列
{
return false;
}
}
return true;
}

Placement functions:

        public  void PlaceQueen_For () 
{
for ( int COL0 = 0 ; COL0 < . 8 ; COL0 ++) // through each row, column 0
{
IF (IsSafe ( 0 , COL0)) //
{
Queen [ 0 ] = COL0; // the safe position storage up, for ( int col1 = 0 ; col1 < . 8 ; col1 ++) // through each column of the first row. 1 { IF (IsSafe ( . 1 , col1)) {




Queen[1] = col1;
for (int col2 = 0; col2 < 8; col2++)
{
if (IsSafe(2, col2))
{
Queen[2] = col2;
for (int col3 = 0; col3 < 8; col3++)
{
if (IsSafe(3, col3))
{
Queen[3] = col3;
for (int col4 = 0; col4 < 8; col4++)
{
if (IsSafe(4, col4))
{
Queen[4] = col4;
for (int col5 = 0; col5 < 8; col5++)
{
if (IsSafe(5, col5))
{
Queen[5] = col5;
for (int col6 = 0; col6 < 8; col6++)
{
if (IsSafe(6, col6))
{
Queen[6] = col6;
for (int col7 = 0; col7 < 8; col7++)
{
if(IsSafe ( . 7 , COL7))
{
Queen [ . 7 ] = COL7;

var A = ( int []) Queen.Clone (); // cloned copy of the current placement method.
List.Add (A); // List of List <int []> structure. Successful placement method is used to store the solution.
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

Analysis: Although the above solution has been able to solve the answer. But scalability is rather poor. If the Queen is 10, 12 Queen do? Furthermore, we found that each of the FOR loop is very similar. The feasibility of using recursion?

    The above cyclic code extracted as follows:

    

        /// <Summary> /// a discharge line queen. /// </ Summary> /// <param name = "Row"> line </ param> public void PlaceQueen ( int Row)         { IF (Row> = Size) // successful solution. N has placed a safe queen. (Size is at the initial queen N)             { // record placement method. int [] cloneQueen = ( int []) Queen.Clone ();                 List.Add (cloneQueen); return ;             } for ( int COL = 0 ; COL <Size; COL ++) // loop over each column of             { 
















if (IsSafe(row, col))
{
Queen[row] = col;
PlaceQueen(row + 1);
}
}
}

  Run PlaceQueen (0) runtime solution can be obtained. 

  Throughout the previous two methods. It is actually a new name. For N> 12 when. Computing a bit slow. Some can be optimized. Such as the use of symmetry, it will traverse the number reduced by half.

  In fact, this function has been optimized a lot. Had the Queen of data storage structure using two-dimensional array. IsSafe () function is more complex.

Analysis of Three

  The above functions, the more time we see the reason for lower consumption. Because it is check all solution, certainly we need to traverse the number n of n-th power. But each time are necessary to determine IsSafe () function. Although the IsSafe () function optimization (Queen only determines the placed.). Really it is necessary to determine what needs each and every time? When the queens placed a row at row col column. Then row + 1 row col-1, col, col + 1 can not recapture. row + 2 row col-2, col, col + 2 can not recapture, and so on.

These positions can recapture if pretreatment. So IsSafe () function will completely unnecessary presence. Each line can hold position already knew in advance.

Cattle were playing: N-bit arithmetic version of the Queen of reference sources  http://www.matrix67.com/blog/archives/266

  c # version of the code

        private static int upperlim = (1 << 8) - 1;// 1111 1111
private static int sum = 0;
public static void Test(int row, int ld, int rd)
{
if (row != upperlim)
{
int pos = upperlim & ~(row | ld | rd);
while (pos != 0 )
{
int p = pos & -pos;//取最近一个可放位置。
POS = POS - P; // remaining positions may be put.
The Test (Row + P, (P + LD) << . 1 , (RD + P) >> . 1 );
}
}
the else
{
SUM ++;
}
}

  To be honest, bit computing version of readability is not really high. But for the N-Queens problem finding such "data structure" to deal with the problem. Really too much bad.

Additional references: http://www.cnblogs.com/jillzhang/archive/2007/10/21/922830.html

  




  

    

   

Reproduced in: https: //www.cnblogs.com/xinjian/archive/2011/10/10/2206498.html

Guess you like

Origin blog.csdn.net/weixin_33904756/article/details/93822269