八皇后问题动态演示_Qt5实现

  1 //核心代码如下
  2 //Queen--放置皇后
  3 
  4 #include "queue.h"
  5 
  6 queue::queue()
  7 {
  8     const int maxn = 9*9;
  9     this->QN = 4;
 10     this->board = new bool[maxn];
 11     for (int i = 0; i < maxn; i++) {
 12         this->board[i] = false;
 13     }
 14     this->judgeRecursion = true;
 15     this->count = 0;
 16 }
 17 
 18 queue::queue(int N)
 19 {
 20     const int maxn = 81;
 21     if (N > 9 || N < 4)
 22         this->QN = 4;    //如果不合法就正规化棋盘
 23     else
 24         this->QN = N;
 25     this->board = new bool[maxn];
 26     for (int i = 0; i < maxn; ++i)  //初始化棋盘,未放置棋子的棋盘设置为false
 27         this->board[i] = false;
 28     this->judgeRecursion = true;
 29     this->count = 0;
 30 }
 31 
 32 bool queue::available (const int Crow, const int Ccol) const  //当前行,当前列
 33 {
 34     for (int hor = 0; hor < Crow; ++hor) {
 35         //纵向查找
 36         if (board[hor * QN + Ccol])     //已经放置皇后的棋盘处为true
 37             return false;               //则返回false--放置不合法
 38     }
 39     int obli = Crow, oblj = Ccol;
 40     while (obli > 0 && oblj > 0) {
 41         if (board[(--obli) * QN + (--oblj)])
 42             return false;              //左斜上查找
 43     }
 44     obli = Crow, oblj = Ccol;
 45     while (obli > 0 && oblj < QN - 1) {
 46         if (board[(--obli) * QN + (++oblj)])
 47             return false;              //右斜上查找
 48     }
 49     return true;                       //都没有,则该位置可以放置皇后
 50 }
 51 
 52 //打印棋盘
 53 void queue::show (bool *Q)
 54 {
 55     const int maxn = 81;
 56     for (int i = 0; i < maxn; i++)
 57         Q[i] = this->board[i];
 58 }
 59 
 60 //重新初始化棋盘
 61 void queue::reset ()
 62 {
 63     const int maxn = 81;
 64     for (int i = 0; i < maxn; i++)
 65         this->board[i] = false;
 66     this->judgeRecursion = true;
 67     this->count = 0;
 68 }
 69 
 70 void queue::reset (int N)
 71 {
 72     const int maxn = 81;
 73     if (N < 4 || N > 9) this->QN = 4;
 74     else
 75         this->QN = N;
 76 
 77     for (int i = 0; i < maxn; i++)
 78         this->board[i] = false;
 79     this->judgeRecursion = true;
 80     this->count = 0;
 81 }
 82 
 83 queue::~queue ()
 84 {
 85     delete []board;
 86     board = nullptr;
 87 }
 88 
 89 /**
 90  * @brief queue::answer --- 放置皇后
 91  * @param solu --- 求解的方法数
 92  * @param Crow --- 当前的行数
 93  * @param Q --- 棋盘,用来打印
 94  */
 95 void queue::answer (int solu, int cur, bool *Q)
 96 {
 97     if (!judgeRecursion)   //递归结束,中断
 98         return;
 99     if (cur == QN) {                  //当前行到最后一行,则一种方案结束
100         count++;
101         if (count == solu) {          //递归到第solu方案时停止
102             this->show (Q);
103             judgeRecursion = false;   //停止递归
104             return;
105         }
106         return;
107     }
108     else
109     {
110         for (int col = 0; col < QN; col++)
111         {
112             if (available (cur, col))         //检查当前行,列
113             {
114                 board[cur * QN + col] = true; //合法则放置皇后
115                 answer (solu, cur + 1, Q);    //递归下一行
116                 //如果回溯法中使用了辅助的全局变量,则一定要及时把它们恢复原状.
117                 //特别的,若函数有多个出口,则需在每个出口处恢复被修改的值
118                 board[cur * QN + col] = false;
119             }
120         }
121     }
122 }

源代码下载地址:链接:https://pan.baidu.com/s/12BTDR8pRMvxpKYNFb988EQ 密码:yk0o

posted @ 2016-10-06 16:30  douzujun  阅读(1254)  评论(0编辑  收藏  举报