Eight Queens (C + + heuristic function Solver)

Source: Internet
Author: User

The eight Queens problem is a typical case of backtracking algorithms, in backtracking, often blindly searching, consuming too much search time. In this experiment, using heuristic search, the search is not either a branch, but the selection of the best branch down search. By defining the state space, operation rules, and search strategy, we can get a solution of the original problem clearly and quickly.

The eight Queens question is a chess-based question: how can you place eight queens on an 8x8 chess board so that no queen can directly eat the rest of the queen? To achieve this, neither of the two queens can be in the same row, vertical line, or slash. By computer programming, we can quickly find solutions to problems.

State space
(i,C[i]), i = 0,1,…,7;(i,C[i])表示第i行的皇后放置在第C[i]列。初始状态为C[i] = -1, i = 0,1,…,7;表示所有行都不放置皇后。目标状态为C[i] != -1, i = 0,1,…,7;表示所有行都已经放置了皇后。
Operation rules
第一个皇后放在第一行;第二个皇后放在第二行且不与第一个皇后在同一列或对角线的空格上;……第i个皇后放在第i行且不与前面i-1个皇后在同一列或对角线的空格上。

Search Policy
由于在某一步放置某个皇后时,可能有多个空格可以使用,所以定义启发式函数:   fx = 剩下未放行中能够用来放皇后的空格数如果第i行的皇后放在第j列合法,计算启发式函数的值fx(i,j)。计算出第i行所有空格的fx后,将第i个皇后放到第i行中那个与前面i-1个皇后不在同一列或对角线上且fx值最大的空格中(相同时取第一个)。如果当前策略无法求解,则回溯至上一步,选择fx值次大的空格放置皇后,依次类推,直至找到一个合法的解。
#include <stdio.h>#include<cstdio>#include<string>#include<math.h>#include<stdlib.h>#include<Set>#include<map>#include<vector>#include<queue>#include<string.h>#include<algorithm>#include<iostream>#include<time.h>#include<list>using namespacestd;Const intn=8;intc[8];//C[i] indicates that the queen of line I is placed in column C[i]intfx[8][8];//Fx[i][j] Indicates the number of spaces in the row that can be put Q after the Queen is placed in row Jintansflag=0;//Mark whether the answer has been foundintvis[3][ -];//Vis[0][j] Indicates that column J has no queen, vis[1][i+j] means diagonal/on and the same), Vis[2][i-j+n] indicates the same difference on the diagonal \, +n avoid negative numbers//heuristic function f (): Find the number of spaces where the remaining lines can put QintFintrow) {    intCnt=0;  for(inti=row+1; i<n;i++)    {         for(intj=0; j<n;j++)        {            if(!vis[0][j]&&!vis[1][i+j]&&!vis[2][i-j+N]) CNT++; }    }    returnCNT;}voidSearchintcur) {    if(cur==N) Ansflag++;//all lines are legally placed Q, end    Else    {        intflag=0;//flag whether the row can be placed Queen         for(intI=0; i<n;i++)//test every space on a cur line        {             if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+N]) {flag=1;//[Cur][i] Place can be placed Qc[cur]=i; vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1; Fx[cur][i]=f (cur);//Calculating heuristic Functionsvis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0; }        }        if(flag)//flag The row can be placed Queen        {             while(!Ansflag) {                intmax=-1; intcol=-1;//record the largest column of FX                 for(intI=0; i<n;i++)//find the largest column of FX                {                    if(fx[cur][i]>max) {Max=Fx[cur][i]; Col=i; }                }                if(max==-1)//The Queen cannot solve any of the spaces in the bank, backtracking{fx[cur-1][c[cur-1]]=-1;//set the original maximum value to 1, then the next backtracking is to find the second-largest value.                     return; } C[cur]=col;//find the largest FX column, place the queen, and search for the next line. vis[0][col]=vis[1][cur+col]=vis[2][cur-col+n]=1; Search (cur+1); vis[0][col]=vis[1][cur+col]=vis[2][cur-col+n]=0; }        }        Else   //flag The row cannot be placed Queenfx[cur-1][c[cur-1]]=-1; }}intMain () {memset (c,-1,sizeof(c)); memset (FX,-1,sizeof(FX)); memset (Vis,0,sizeof(VIS)); Search (0);  for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++)        {            if(j==C[i]) cout<<"Q"<<' '; Elsecout<<"X"<<" "; } cout<<Endl; }}

To solve the corresponding chessboard:
Q x x x x x x x
x x x x x Q x x
x x x x x x x Q
x x Q x x x x x
x x x x x x Q x
x x x Q x x x x
x Q x x x x x x
x x x x Q x x x

Eight Queens (C + + heuristic function Solver)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.