8皇后问题的传统解决方法

#include 
#include 
using namespace std;

void print_information();
void solve_from(Queens & configuration);

int main()
/* Pre: The user enters a valid board size.
*  Post: All solutions to the n-queens puzzle
*        for the selected board size are printed.
*  Uses: The class Queens and the recursive function slove_from.
*/
{
    int board_size;
    print_information();
    cout<<"What is the size of the board?"<>board_size;
    if(board_size <0 || board_size>max_board){
        cout<<"The number must between 0 and "<


Queens类


#ifndef QUEENS_H
#define QUEENS_H
#include 
using namespace std;
const int max_board = 30;
class Queens
{
public:
    Queens(int size);
    bool is_solved() const;
    void print(); //要记录执行了多少次打印
    bool unguarded(int col) const;
    void insert(int col);
    void remove(int col);
    int board_size;
private:
    int count;
    bool col_free[max_board];
    bool upward_free[2 * max_board -1];
    bool downward_free[2*max_board-1];
    int queen_in_row[max_board]; // column number of queen in each row.
public:
    int numOfSolutions;
};

#endif // QUEENS_H


Queens 类方法的实现:


#include "Queens.h"

Queens::Queens(int size)
/* post: 初始化一个size*size的空的棋盘
*/
{
    board_size = size;
    count = 0;
    numOfSolutions = 0;
    int i;
    for(i=0;i








你可能感兴趣的:(数据结构学习)