Data Structure: backtracking and tree traversal, data structure backtracking
Evaluate the power set of a set containing n elements (for details, see section 6.7 of Yan Weimin data structure)
/// // Evaluate the idempotence (depth first traverses the solution space) # include <iostream> # include <vector> using namespace std; void GetPowerSet (const vector <int> & SrcVec, vector <int> & DstVec, int I) {if (I> = SrcVec. size () {if (DstVec. empty () cout <"empty set" <endl; else {for (int j = 0; j <DstVec. size (); ++ j) cout <DstVec [j] <""; cout <endl ;}} else {// Add the current element DstVec. push_back (SrcVec [I]); GetPowerSet (SrcVec, DstVec, I + 1); // do not add the current element DstVec. pop_back (); GetPowerSet (SrcVec, DstVec, I + 1) ;}} int main () {vector <int> SrcVec; SrcVec. push_back (1); SrcVec. push_back (2); SrcVec. push_back (3); SrcVec. push_back (4); SrcVec. push_back (5); vector <int> DstVec; GetPowerSet (SrcVec, DstVec, 0); return 0 ;}
Question 4 Queen (for details, see section 6.7 of Yan Weimin data structure)
/////////// Solve the n queen's problem by backtracking /// # include <iostream> # include <iomanip> using namespace std; # define N8 // board size int gChessboard [N] [N]; // checker NxNint gResultCount; // constraints: two pawns do not occupy the same row or column on the board, or the same diagonal line. // Note: When you enter this function, i-1 pawns that do not attack each other have been placed on the I-1 line of the Board. Bool CheckConstraint (int I, int j) {int m = 1; for (int k = I-1; k> = 0; -- k) {// check whether the preceding row has an if (gChessboard [k] [j]> 0) return false; // check whether the preceding row is on the first diagonal line. if (j-m)> = 0 & gChessboard [k] [j-m]> 0) return false; if (j + m) <N & gChessboard [k] [j + m]> 0) return false; ++ m;} return true ;} // output the void OutPutChessboard () {cout <left; for (int I = 0; I <N ;++ I) {for (int j = 0; j <N; ++ j) {cout <setw (2) <(gChessboard [I] [j] = 0 )? "0": "x") <";}cout <endl ;}cout <" --------------------------------------------- "<endl; cout <right ;} // tentatively insert row I into the void Trial (int I) {if (I> = N) {// output the valid checkerboard layout OutPutChessboard (); // count the number of solutions gResultCount ++;} else {for (int j = 0; j <N; ++ j) {// in row I, put the j column in the chess piece gChessboard [I] [j] = 1; if (CheckConstraint (I, j) Trial (I + 1 ); // The current layout is valid //.... invalid. Perform pruning (technically, it is simply understood that this line is not searched). // put row I before removing the line, j-column chess piece gChessboard [I] [j] = 0 ;}} int main () {// solve N queen Trial (0 ); cout <N <" ---- Number of searched solutions:" <gResultCount <endl; return 0 ;}
Author: Hill er
Mark the source for reprinting. Thank you. Address: http://blog.csdn.net/s634772208/article/details/46682283
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.