Eight Queens problem - backtracking solution

Eight queens problem : place eight queens on the 8 × 8 grid of chess, it can not attack each other, that is, any two queens can not be in the same line, same column or the same slash and asked how many pendulum method .

int g_number = 0; // how many display methods
 
void EightQueen ()
{
   const int = Queens. 8; // board size
 
   int ColumnIndex [queens]; // column index
 // iterate row
   for (int i = 0; i < Queens; ++ I)
 
       the ColumnIndex [I] = I;

   Permutation(ColumnIndex, queens, 0);
}
 
void Permutation(int ColumnIndex[], int length, int index)
{
   if(index == length)
   {   //满足条件
       if(Check(ColumnIndex, length))
       {
           ++ g_number;  //摆法数+1
           PrintQueen(ColumnIndex, length);  //输出
       }
   }
   else
   {
       for(int i = index; i < length; ++ i)
       {   
           int temp = ColumnIndex[i];
           ColumnIndex[i] = ColumnIndex[index];
           ColumnIndex[index] = temp;
 
           Permutation(ColumnIndex, length, index + 1);

           the ColumnIndex = TEMP [index];
 
           the ColumnIndex [index] = the ColumnIndex [I];
 
           the ColumnIndex [I] = TEMP;
       }
   }
}
// pruning function !!!!!!!!
BOOL the Check (the ColumnIndex int [], int length )
{
   for (int I = 0; I <length; I ++)
   {
       for (int I = J +. 1; J <length; J ++)
       {// determines whether hatched in the same column or the same
           if (( I - the ColumnIndex == J [I] - the ColumnIndex [J])
               || (J - I == the ColumnIndex [I] - the ColumnIndex [J]))
           return to false;
       }
   }
   return to true;
}

 // output display g_number and ColumnIndex [i] corresponding to the value of the
void PrintQueen (the ColumnIndex int [], int length)
{
   the printf ( "% Solution D \ n-", g_number); // Print Solutions
   // print all the column index value
   for (int I = 0; I <length; ++ I)
 
       the printf ( "% D \ T", the ColumnIndex [I]);
 
   the printf ( "\ n-");
}


What is backtracking:
search process similar attempt to enumerate, and when found to have been solved condition is not satisfied, on the "back" to return to try a different path.
 
Backtracking is an optimal selection search method, search forward by optimal selection conditions to achieve the goal. But when a step to explore, discover original choice is not superior or to meet its target, it is a step back to re-select, this dead end on the return walk technology for backtracking.
 
The general steps to solve the problem:
1, for the to the problem, the solution space problem definition, it contains at least one (optimal) solution to the problem.
2, the structure easy to search the solution space is determined such that can backtracking easily search the entire solution space.
3, depth-first search of the solution space, and a function to prevent invalid pruning in the search process using the search
 
 
backtracking Detailed:
backtracking to start from the node (root node), the depth-first search the entire solution space . The start node becomes a slipknot point, but also become an extension of the current node. At the current extension node, the search moves to a new node in the depth direction. The new node will become a new point of slipknot, and become the extension node. If the mobile can no longer extend in the depth direction at the current node, the current node becomes extended knot point. In this case, it should be moved back (back) at a slipknot to the nearest point, and this point becomes the current extension slipknot node. Backtracking that is this way of working recursively search in the solution space until it has been so far there is no point slipknot find the required solution or the solution space.

Guess you like

Origin www.cnblogs.com/fightingcode/p/10991995.html