/*dfs, the key: Check the status of the Queen will attack. Two methods: (Second, faster) (1) void Solve (int row, int colused); General approach, the method of judging whether the state is illegal: column through the bitwise operation; left oblique, right oblique through traversal.
(2) void solvebitoperation (unsigned col, unsigned tiltleft, unsigned tiltright), full-bit operation, specific reference code comment. Also can refer to Matrix67 's blog (with image, very image):http://www.matrix67.com/blog/archives/266C + + implementation reference:http://www.cnblogs.com /lee41sum/archive/2010/04/22/1717986.html* *
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstddef>5#include <iterator>6#include <algorithm>7#include <string>8#include <locale>9#include <cmath>Ten#include <vector> One#include <cstring> A#include <map> -#include <utility> -#include <queue> the#include <stack> -#include <Set> - using namespacestd; - Const intINF =-0x3f3f3f3f; + Const intMAXN = -; - Const intModprime =3046721; + A intN; at BOOLisusednode[ -][ -]; - intAnswer =0; - - //----------------------------------method One---------------------------------- - BOOLIsattack (intRowintColintcolused) - { in if(colused& (1<<col)) - { to return true; + } - for(inti = row-1, j = col-1, k = col +1; i >0; -------j, + +k) the { * if(J >0) $ {Panax Notoginseng if(Isusednode[i][j]) - { the return true; + } A } the if(k <=N) + { - if(Isusednode[i][k]) $ { $ return true; - } - } the } - return false;Wuyi } the - voidSolve (intRowintcolused) Wu { - if(Row = = n+1) About { $++answer; - return; - } - for(intCol =1; Col <= N; ++Col) A { + if(!isattack (Row, col, colused)) the { -Isusednode[row][col] =true; $Solve (Row +1, colused | (1<<col)); theIsusednode[row][col] =false; the } the } the } - in //----------------------------------method and---------------------------------- theunsigned limN;//N Queens Target State (all columns already have queens, note: Columns here are compressed n rows into a row, with N columns in total) the voidsolvebitoperation (unsigned col, unsigned tiltleft, unsigned tiltright) About{//The illegal position of an illegal position created by a left oblique. the if(col = =LimN) the { the //all columns already have Queens +++answer; - return; the }Bayiunsigned freeposset = limn& (~ (Col | tiltleft | tiltright));//get a collection of columns that do not conflict the while(Freeposset) the { -unsigned freepos = freeposset& ((~freeposset) +1); - //in the column collection that does not conflict, get the rightmost column position (if you can't think of the reason for the statement, take an example and deduce it yourself) the /* the eg: the freeposset binary: 01010010 the ~freeposset binary: 10101101 - (~freeposset) +1 binary: 10101110 the freeposset& ((~freeposset) + 1) binary: 00000010 the the */94Freeposset-=Freepos; the //In a column collection without conflicts, remove the position of the column where the current row has been used theSolvebitoperation (Col | freepos, (tiltleft | freepos) <<1, (Tiltright | freepos) >>1); the //to the next line: Add an illegal column position (that is, the column position of the current row queen)98 //because the current line more than a queen, so left oblique to the next line more an illegal position, right oblique to the next line more than an illegal position About } - }101 102 intMain ()103 {104 #ifdef HOME theFreopen ("inch","R", stdin);106 //freopen ("Out", "w", stdout);107 #endif108 109memset (Isusednode,false,sizeof(Isusednode)); theCIN >>N;111 //Method One the //Solve (1, 0);113 //Method Two theLimN = (1<< N)-1; theSolvebitoperation (0,0,0); thecout << Answer <<Endl;117 118 #ifdef HOME119Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl; - _CrtDumpMemoryLeaks ();121 #endif122 return 0;123}
Depth-First search Code[vs] 1295 n Queen question