A case study of C + + backtracking method _c language

Source: Internet
Author: User

This article describes the retrospective method of C + +, shared for everyone to use for reference. The specific methods are analyzed as follows:

In general, backtracking is a systematic method of enumerating all possible states in the state space, which is a general algorithm framework.

Solution Vector a= (A1, A2, ..., an), where each element ai is taken from a finite sequence set of SI, such a solution vector can represent an arrangement in which the AI is the first element of the permutation, or a subset of S, where the AI is true and only when the first element of the complete collection is in S Can even represent the sequence of actions of the game or the path in the diagram.

At every step of the backtracking method, we a={a1 from a given part, A2, ..., AK} start, try to add the element to extend this partial solution, after the extension, we have to test whether it is a complete solution, if so, to output the solution; We must examine whether this partial solution is still possible to extend into a full solution, if possible, recursively; if it is not possible, remove the last element of the new join from a, and then try other possibilities at that location.

Using a global variable to control whether backtracking is complete, this variable is set to Finished, then the backtracking framework is the essence and artifact of backtracking Dafa

bool finished = false;

void Backtack (int input[], int inputsize, int index, int states[], int statesize)
{
 int candidates[maxcandidate];
 int ncandidates;

 if (issolution (input, inputsize, index) = = True)
 {
 processsolution (input, inputsize, index);
 }
 else
 {
 constructcandidate (input, inputsize, index, candidates, &ncandidates);
 for (int i = 0; i < ncandidates i++)
 {
  Input[index] = candidates[i];
  Backtack (input, inputsize, index + 1);
  if (finished) return;}}


Not rigidly confined to the form of frames, we can write the following code:

#include <iostream>

using namespace std;

Char str[] = "abc";
const int size = 3;

int Constructcandidate (bool *flag, int size = 2)
{
 flag[0] = true;
 FLAG[1] = false;

 return 2;
}

void Printcombine (const char *STR, BOOL *flag, int pos, int size)
{
 if (str = NULL | | flag = NULL | | | Size < = 0) return
 ;
 
 if (pos = size)
 {
 cout << "{";
 for (int i = 0; i < size; i++)
 {
  if (flag[i] = = True)
  cout << str[i] << "";
 }
 cout << "}" << Endl;
 }
 else
 {
 bool candidates[2];
 int number = Constructcandidate (candidates);
 for (int j = 0; J < number; J +)
 {
  Flag[pos] = candidates[j];
  Printcombine (STR, flag, POS + 1, size);
 }
 }

void Main ()
{
 bool *flag = new Bool[size];
 if (flag = NULL) return
 ;
 Printcombine (STR, flag, 0, size);
 delete []flag;
}

A retrospective framework is used to compute the dictionary order:

#include <iostream> using namespace std;
Char str[] = "ABC";

const int size = 3; void Constructcandidate (char *input, int inputsize, int index, char *states, char *candidates, int *ncandidates) {if (in put = = NULL | | Inputsize <= 0 | | Index < 0 | | Candidates = = NULL | |
 
 Ncandidates = = NULL) return;
 BOOL buff[256];
 for (int i = 0; i < 256 i++) buff[i] = false;
 int count = 0;
 for (int i = 0; i < index; i++) {Buff[states[i]] = true;
 for (int i = 0; i < inputsize i++) {if (buff[input[i]] = = False) candidates[count++] = Input[i];
 } *ncandidates = count;
Return
 BOOL Issolution (int index, int inputsize) {if (index = = inputsize) return true;
else return false;

 } void Processsolution (char *input, int inputsize) {if (input = = NULL | | inputsize <= 0) return;
 for (int i = 0; i < inputsize i++) cout << input[i];
cout << Endl; } void Backtack (char *input, int inputsize, int index, char *states, int statesize) {iF (input = = NULL | | inputsize <= 0 | | | Index < 0 | | | states = NULL | | statesize <= 0) return;
 Char candidates[100];
 int ncandidates;
 if (issolution (index, inputsize) = = True) {processsolution (states, inputsize);
 Return
 else {constructcandidate (input, inputsize, index, states, candidates, &ncandidates);
  for (int i = 0; i < ncandidates i++) {States[index] = candidates[i];
 Backtack (input, inputsize, index + 1, states, statesize);
 } void Main () {char *candidates = new Char[size];
 if (candidates = NULL) return;
 Backtack (str, size, 0, candidates, size);
delete []candidates;

 }

Comparing the two cases above, you can see that the only difference is that the whole permutation is not required for the current solution vector, and the dictionary order is required for the current solution vector and needs to know the state of the current solution!
Eight queen backtracking algorithm:

#include <iostream> using namespace std;

int position[8];  void constructcandidate (int *input, int inputsize, int index, int *states, int *candidates, int *ncandidates) {if (input = = NULL | | Inputsize <= 0 | | Index < 0 | | Candidates = = NULL | |
 
 Ncandidates = = NULL) return;
 *ncandidates = 0;
 BOOL Flag;
 for (int i = 0; i < inputsize i++) {flag = true;
  for (int j = 0; j < index; J +) {if (ABS (INDEX-J) = ABS (I-STATES[J)) flag = false;
 if (i = = States[j]) flag = false;
  } if (flag = = True) {Candidates[*ncandidates] = i;
 *ncandidates = *ncandidates + 1;
 }/* cout << "ncandidates =" << *ncandidates << Endl;
System ("pause");
 BOOL Issolution (int index, int inputsize) {if (index = = inputsize) return true;
else return false;

} void processsolution (int &count) {count++;} void Backtack (int *input, int inputsize, int index, int *states, int statesize, int &count) {if (input = = NULL | | Inputsize <= 0 | | Index < 0 | | states = = NULL | |
 
 Statesize <= 0) return;
 int candidates[8];
 int ncandidates;
 if (issolution (index, inputsize) = = True) {processsolution (count);
 else {constructcandidate (input, inputsize, index, states, candidates, &ncandidates);
  for (int i = 0; i < ncandidates i++) {States[index] = candidates[i];
 Backtack (input, inputsize, index + 1, states, Statesize, count);

 } void Main () {//Initialize chess for (int i = 0; i < 8; i++) position[i] = i;
 int states[8];
 int count = 0;
 Backtack (position, 8, 0, states, 8, count);

 cout << "Count =" << count << Endl;}

I hope this article will help you with your study of C + + program design.

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.