Backtracking is the algorithm is a control strategy in the search algorithm, is a heuristic process. In the process of testing, if you encounter the wrong choice , you will go back to the previous step and continue to choose the next way, step by step until you find the solution or prove that there is no solution.
Here is a classic backtracking question n Queen's solution tree:
Let's start with the N queen:
"Problem description"
On NXN's chess plate, place n queens so that no queen can eat another. The condition to be fulfilled is that there can be only one queen on the same line, in the same column, on the same diagonal. All the placement scenarios that meet the requirements. The 4 Queen placement scheme is as follows:
"Enter" a positive integer n.
Each row of "output" represents a placement scheme: the first number of line I is I, which means I scheme, followed by a colon, and then the number of n separated by a space, where the number of I x[i] means the Queen on line I is placed in the X[i] column; the last line: An integer that represents the total number of scenarios.
"Sample Input"
4
"Sample Output"
1:2 4 1 3
2:3 1 4 2
2
The topic is simply too classic, and the first thing that comes to mind when you think about backtracking is it. This question is very easy to understand, that is, in a n*n board to meet the conditions to place the N queen, each combination of search again. In the process of searching to determine whether the next step of the direction can go, if you can enter the next layer, if not meet the requirements of the search for a direction, after searching all the direction of this layer back to the previous layer, continue to search the next layer of direction.
Do not know that we understand the no, if not understand, read the code to understand.
On the code:
Take the idea to the code and you should understand it.
Backtracking is such a layer of search, find the end of the layer to return to the previous layer continue to find, until the correct solution is found.
Continue to read another question:
3, Prime Chain
The design program will be 1 ... n rows into a row, so that any two contiguous number of the and is a prime. The 1th and the last and also the prime number. Output a scenario.
Input:
N (n<=100)
Output:
A sequence of 1 to N, separated by a space in the middle. The first number is 1.
If no solution output-1.
Sample input:
10
Sample output:
1 2 3 4 7 6 5 8 9 10
This problem and just that N Queen's train of thought is the same, traverse all possible, in the process of traversal judgment, if you can continue to search the next layer.
The code is as follows:
#include <cstdio> #include <iostream>const int maxx=1000;using namespace Std;int n;int numguo[maxx]={0};bool guo[maxx]={0},pguo=0;int c=0;int sushu (int x)//function to verify prime number {//The question of judging primes has been done very early, I'm not going to explain here. int I=2;while (i<x) {if (x%i==0) return 0;//if not directly out of the 0i++;} Return 1;//If yes returns 1}int Panduan () {int t;for (int i=1;i<n;i++)//determine if the requirement {if (!sushu (numguo[i]+numguo[i+1]))// I'll just put it in the function to verify that {return 0;//, if it's not a prime, jumps back 0}}if (!sushu (numguo[n]+numguo[1])) return 0;//take care not to forget the last and first digits and whether the number is a prime return 1 ;//correct words return 1}int print () {c++;//possible method +1for (int i=1;i<=n;i++) {cout<<numguo[i]<< ';} Cout<<endl;} int search (int x) {for (int i=1;i<=n;i++)//Search this layer for each of the possible {if (!guo[i]) {guo[i]=1;//Occupy numguo[x]=i;//tag if (x==n)//If the quantity requirement is reached {if (Panduan ())//If the requirements {if (!pguo)//If not output {pguo=1;//output over print ();//output return 0;//search directly out of}}}else search (x+1); guo[i]=0; numguo[x]=0;}}} int main () {cin>>n;search (1);//start with the first to find if (!c) cout<< "1";//if it is not possible to output "1" return 0;}
I used a lot of functions in these codes, which saves code length and is more convenient (personally).
Backtracking is the center of thought, step by step search, a layer of search, until the search or find the results.
In fact, the above two questions are the basis of water, and then back to traverse graphs, trees and so on.
Ending ...
Backtracking algorithm ———— n queens, prime string