The BackTracking Algorithm for n Queen Problem

xiaoxiao2021-03-05  26

The article is about the design of algorithm for n queen problem The intention of this article is from an excise of an algorithm book:. << Introduction to the design and analysis of algorithms >> This backtracking algorithm is interesting, so I decide to. record the process of implementation. First, I use a double dimension array to simulate the chess board. But quickly I have found this form of representation is not so convenient especially in the check function that is used to checking whether or not current solution is promising Because The CHECK FUNCY, The Value of Array Is TriVial. IE A [0] [0] = 1 represent (0,0) Position of chess board HAS a chess and the next shus can't BE POSION (0). The Pair (0) Already Indicate Position (0) HAS a Chess, IT IS Unnecessary to Assigning a value 1 to Pair (0) to Denoting (0) HAS CHESS. SO I HAVE CHANGED A Reperesentation of Chess position by Vector >. Apparently, Pair stands for (x, y). Notice that a solution of n queen require each row is different. That is, for a solution of n queen, each of pair_instance.first must distinct. This simplifies the representation further. It just needs A vector to represent a chess board. so The Solution of N Queen Problem Can Be Viewed As a Permutation of Natural Number from 0 to N-1. Apparently, The Brute-Force Algorithm Has Efficiency ω (N!).

For the Part of Check Function, I Have Observed The Characteristic of Permutation That Is Confirmed with N Queen Problem. The rule is defined as Follow:

If The Number of Position P in Permutation IS i, THE VALE OF POSITION q (Denoted BY J) CAN't BE Q-P i OR P-Q I. (Q ∈ (p, n-1]); Revist: (0) (1, 2) (2, 4) (3, 3) (4, 1) IS A ILLEGAL SOLUTION WHICH CAN Be Viewed AS 0, 2, 4, 3, 1Simply. 4 (i) In Position 2 (P), 3 (Q's Vaule: J) in Position 3 (Q) ------ q-p i = 5 (no vioc); P-Q I = 3 (Viocation). Starting from Zero)

It requires a two nested for loop to attaining. In addition, if there exist a method to constructing a NFA to recognize the permutation, the n queen problem algorithm will have Θ (n) efficiency class. But it seems impossible to do that because the Fa Needs Counting and no Way to Abstract States.

The Codes:

#include #include #include #include #include #include using namespace std; template struct output_functor: public unary_function { Void Operator () (Const T & P) const {cout << "(" << p.first << "," << p.second << ")" << "";}}; class chess_board {Vector < PAIR > position; vector array; vector duplicate_array; public: int n; int cur_i; int number_solution; chess_board (int Dim): N (DIM), CUR_I (0), Number_Solution 0) {position.reserve (n); array.resize (n, -30000); // if using -1 to Denoting No Chess, IT Will ContraDict with the Diagonal Test. IE Move {0,0} .The Content of Array IS {0, -1, -1, -1} .The Diagonal Test Will Return False. SO I Use -30000 to Express the meaning, but it is still imperfect. it is required a number to Denoting Impossible Number or Infinity Simply Duplicate_ar Ray.resize (n, 0); for (int i = 0; i

Else duplicate_Array [J] = 1;} Bool Check () {Vector :: Iterator i = std :: find (duplicate_array.begin (), duplicate_array.end (), 2); if (i! = duplicate_Array. End ()) Return False; if (Check_Permutation (array) == false) Return False; return true;} void write () {Number_solution ; for_each (position.begin (), position.end (), Output_functor > ()); cout << end1;} private: bool check_permutation (const vector & p) {for (INT i = 0; i

J > n; chess_board c (n); clock_t t1 = clock (); backtracking (c); cout << "The number of solutions is:" << c.Number_solution << Endl; Clock_t t2 = clock (); cout << "Times TOOK:" Double (T2-T1) / clocks_per_sec; System ("pause"); Return 0;} Note That this is Just A "Pure" Backtracking Algorithm and It Has NOT CONSIDED The Optimization. I Will Optimize It Later.

转载请注明原文地址:https://www.9cbs.com/read-39332.html

New Post(0)