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

来源:互联网 发布:免费下载淘宝返还网 编辑:程序博客网 时间:2024/05/21 06:50

#include <iostream>#include <Queens.h>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?"<<flush;    cin>>board_size;    if(board_size <0 || board_size>max_board){        cout<<"The number must between 0 and "<<max_board<<endl;    } else {        Queens configuration(board_size);        solve_from(configuration);        cout<<endl<<"一共有"<<configuration.numOfSolutions<<"个解"<<endl;    }    return 0;}void print_information(){    cout<<"这个一个n皇后解的个数的求解函数。"<<endl;    cout<<"你输入问题的规模n,我输出一个合适的解答"<<endl;}void solve_from(Queens & configuration)/* Pre: the Queens configuration represents a partially completed arrangement*       of nonattacking queens on a chessboard.*  Post: All n-queens solutions that extend the given configuration are printed.*        The configuration is restored to its initial state.*  Uses: The class Queens and the function solve_from, recursively.*/{    if(configuration.is_solved()) configuration.print();    else {        for(int col=0;col<configuration.board_size;col++){            if(configuration.unguarded(col)){                configuration.insert(col);                solve_from(configuration);                configuration.remove(col);            }        }    }}


Queens类


#ifndef QUEENS_H#define QUEENS_H#include <iostream>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<board_size;i++) col_free[i] = true;    for(i=0;i<board_size * 2 - 1;i++) {            upward_free[i] = true;            downward_free[i] = true;    }}bool Queens::is_solved() const/* postcondition:*     如果棋盘上现在已经有board_size个的皇后,那么就成功了,返回true。        否则返回false*/{    return count==board_size;}void Queens::print()/* post 把棋盘上皇后的分布打印出来*/{    for(int row=0;row < board_size;row++){        for(int col = 0; col < board_size; col++){            if(queen_in_row[row]==col) cout<<"X";            else cout<<"O";        }        cout<<endl;    }    cout<<endl;    numOfSolutions++;}bool Queens::unguarded(int col) const/*PostCondition :判断在数组的(count,col)这个位置可不可以放一个皇后* 如果可以放一个皇后的话,那就返回true,否则返回false*/{    return col_free[col]            && upward_free[count+col]            && downward_free[count-col+board_size-1];}void Queens::insert(int col)/* Pre: nested-arrays' position [count][col]        was not guarded by any existed queens.   Post: Then We put the queen at position         [count][col] and increase the count by 1*/{    queen_in_row[count] = col;    col_free[col] = false;    upward_free[count+col] = false;    downward_free[count-col+board_size-1] = false;    count++;}void Queens::remove(int col)/* Pre: at [count-1][col] there is a queen.*  Post: We delete the queen at the position above,*        at the meanwhile, we decrease the count by 1.*/{    count--;    col_free[col] = true;    upward_free[count+col] = true;    downward_free[count-col+board_size-1] = true;}








0 0
原创粉丝点击