Introduction to the algorithm Classic-seventh chapter example 7-4-1 expansion n Queen problem backtracking method

Source: Internet
Author: User
Tags cmath

In fact the backtracking method has the meaning of brute force to solve a problem, go all the way to the end, the road can not pass, return to find another road.

Backtracking can solve a lot of problems, such as:n Queen problem and maze problem.

I. Concept

The backtracking algorithm is actually similar to the enumeration of the search attempt process, mainly in the search attempt to find the solution of the problem, when found not satisfied with the condition, then back, try another path.

Baidu explained: Backtracking method (exploration and backtracking) is an optimal search method, also known as the Heuristic method, according to the selection criteria to search forward to achieve the goal. However, when the exploration of a step, found that the original choice is not good or not reach the goal, then return to the one-step re-selection, such a failure to return to go back to the technology as backtracking, and meet the backtracking condition of a state point of the "backtracking point."

Two. Basic ideas

In the solution space Tree of all solutions containing the problem, the solution space tree is explored in depth from the root node based on the strategy of depth-first search . When exploring a node, it is necessary to determine whether the node contains the solution of the problem, if it is included, to continue the exploration from the node, if the node does not contain the solution of the problem, it will go back to its ancestor node by layer. (In fact, the backtracking method is the depth-first search algorithm for implicit graphs).

If you use backtracking to find all the solutions to the problem, go back to the root, and all the viable subtrees of the root node are searched and finished.

If you use backtracking to find any solution, you can end up searching for a solution to the problem.

Three. N Queen's question

///The/ n Queen is a typical DPS problem, which is implemented recursively, and the output has to store the result of the ancestor operation. #include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<cctype>#include<cmath>#include<map>#include<Set>#include<vector>#include<queue>#include<stack>#include<ctime>#include<algorithm>#include<climits>using namespacestd; Const intn=101; Static intdisp[ -];intC[n]; inttot; intN; voidSearchintcur) {    if(cur==N) {             for(inti =0; i<n; i++) printf ("%d", Disp[i]); printf ("\ n"); Tot++; }    Else      for(intI=0; i<n;i++)    {        intok=1; C[cur]=i;//try to put the Queen of cur in column I.         for(intj=0; j<cur;j++)//Check if there's a clash with the Queen.        if(c[cur]==c[j]| | cur-c[cur]==j-c[j]| | cur+c[cur]==j+C[j]) {OK=0; Break; }                if(OK) {Disp[cur]=i;//the current line of the Queen in the first few columnsSearch (cur+1); } } }intMain () { while(cin>>N) {tot=0; Search (0); cout<<tot<<"kind of Method"<<Endl; }      return 0; }   

#include <algorithm>#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>using namespacestd;intc[ Max],vis[3][ Max],tot,n=8, Sum_max;intmapn[9][9];voidSearchintcur) {    if(cur==n)//recursive boundaries, as long as they come here, all queens must not conflict.    {        if(sum_max<tot) Sum_max=tot; }    Else  for(intI=0; i<n;i++)    {        if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n])//directly judging by two-dimensional array{//0 for vertical lines, 1 for diagonal, 2 for main diagonalC[cur] = i;//Save the position of the Queen on each lineTot + =Mapn[cur][i]; vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] =1; Search (cur+1); vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] =0;//Remember to change it back .Tot-=Mapn[cur][i]; }    }}intMain () {intT; CIN>>T;  while(t--) {Sum_max=0, tot =0; memset (Vis,0,sizeof(VIS));  for(intI=0;i<8; i++)             for(intj=0;j<8; j + +) {cin>>Mapn[i][j]; } Search (0); printf ("%5d\n", Sum_max); }    return 0;}

Introduction to the algorithm Classic-seventh chapter example 7-4-1 expansion n Queen problem backtracking method

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.