Board Placement queen problem (retrospective thinking of the classic application)

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.