4 Queens and 8 Queens The problem is one of the classical applications of backtracking thought, and its core thought is still the depth-first search of the solution space tree. Today, in the process of blind writing code actually made a mistake, and then meditation down, carefully review the algorithm every step to write the correct code. My summary is that the essence of the backtracking algorithm is to determine the solution space tree, establish the recursive start and end conditions, in order to reduce the complexity of time pruning, backtracking to return to the problem is not modified state. Clear the above 4 problems can solve the problem smoothly.
The solution space tree is as follows,
The detailed code is as follows,
QueenSettle.cpp:Defines the entry point for the console application.
#include "stdafx.h" #include <iostream> #define PSIZE 4 using namespace std; Decide whether to place the new queen in panel[h][w]//inline bool isconflict (int **panel,int len,int H, int. W) {for (int i=0;i
The operation results are as follows