# Include <iostream> # include <vector> using namespace STD; # define maxnum 10int tot = 0, row, line [maxnum], n = 8; void search (INT row) // recursive search for feasible solutions {int I, j; If (ROW = N) tot ++; // when ROW = N, the queen of each row does not conflict, that is, the feasible solution else for (I = 0; I <n; I ++) {int OK = 1; line [row] = I; // try to put the queen of row in column I for (j = 0; j <row; j ++) // check whether it is in conflict with the Queen. {If (line [row] = line [J] | line [row]-line [J] = row-j | | line [J]-line [row] = row-j) {OK = 0; break; // if a conflict exists, stop Search: returns the first-level Recursive Backtracking. The key to efficient backtracking.} If (OK) Search (row + 1) ;}} const int n = 8; // recursive int CNT = 0; void DFS (INT layer, int Col, int left, int right) {If (n = Layer) {CNT ++; return;} int TMP = ~ (COL | left | right); // bitwise result! For (INT I = 0; I <n; I ++) {If (TMP & (1 <I )) // Yes & No & {int T1 = Col | (1 <I), T2 = (left | (1 <I)> 1, t3 = (right | (1 <I) <1; // DFS (layer + 1, Col & (1 <I ), (left & (1 <I) <1, (Right & (1 <I)> 1); DFS (layer + 1, T1, T2, t3) ;}}int main () {tot = 0; n = 8; search (0); cout <tot <Endl; DFS (0, 0, 0 ); cout <CNT <Endl; System ("pause"); Return 0 ;}