/* * Eight queens Problem backtracking programming exercise * on the 8x8 chessboard, place 8 queens, two Queens cannot 22 attack * Also, straight line, vertical 45 degrees, 135 degrees direction cannot appear two queens * Copyright Michael 2014-12-19 * QQ 119206 5414 **/#include <iostream> #include <stack> #include <stdlib.h> #include <string.h>using namespace std;struct sposition{int iRow; int icolumn;};/ * * Save results using stack **/stack<sposition> resultstack;/* * Judging in horizontal direction x, vertical direction Y, whether you can place new Queen **/bool judgeisacceptable (int x, int y) { struct Sposition pos; Stack<sposition> ResultS (Resultstack); int stackSize = Results.size (); for (; stackSize > 0;--stacksize) {pos = Results.top (); if (Pos.irow = = x)//Determine if there is already a queen {return false on the same line; } if ((x-pos.irow) = = (Y-pos.icolumn))//Determine if the 45 degree angle already has the Queen {return false; } if ((x-pos.irow) = = (pos.icolumn-y))//judgment-45 degree angle has the Queen {return false; } results.pop (); } return true; /* Queen Placement algorithm * 0 0 0 ... * 0 0 0 ... * 0 0 0 ... * ...... *Traverse the No. 0 column, take the first, second of the No. 0 column ... * Start with column 1th and try the 2nd column, when trying to a column where 8 rows cannot be placed Queen, then backtrack, return to the next row of the previous column to continue trying **/void solvequeue () {sposition pos; for (int i = 0; i < 8; ++i) {//the first line of the queue pos.irow = i; Pos.icolumn = 0; Resultstack.push (POS); int x = 0; Mark the current line, 0~7 line int y = 1; Mark the forefront, 0~7 column while (y<8)//Start with the first column {for (; x < 8; ++x)//start exploring from line No. 0 { if (judgeisacceptable (x, y)) {pos.irow = x; Pos.icolumn = y; Resultstack.push (POS); The placement is complete, the output is the IF (8 = = Resultstack.size ()) {while (! Resultstack.empty ()) {pos = Resultstack.top (); cout<<pos.irow<< "\ T" <<pos.iColumn<<endl; Resultstack.pop (); }} x = 0; This column is placed to finish, continuing the next column to place a break; }} if (8 = = x)//This column, 8 rows can not be placed Queen, back to the next line of the previous column {pos = Resultstack.top (); x = pos.irow+1; if (8 <= x) {y = y-2; If the previous column is placed in the last column, you do not need to continue the attempt, you should backtrack two columns, and continue to try Resultstack.pop () from the first two columns; pos = Resultstack.top (); x = pos.irow+1; } else {--y; Go back to the next row in the previous column to continue the attempt} resultstack.pop (); Continue Control the number of columns, do not perform ++y operation} ++y;; } if ((8 = = y) && (resultstack.size ()! = 8))//If 8 rows have been tried, but not enough 8 rows, error status, handle the next possibility {WHI Le (! Resultstack.empty ()) {Resultstack.pop (); }} cout<<endl; Cout<<endl; }}int Main () {Solvequeue ();}
8 Queen-----Backtracking C + + programming exercises