C++ recursive implementation of n queen problem code of eight queen problem

  • 2020-04-02 02:04:23
  • OfStack

Let's start with the basic 8 queens question:

Put eight queens on an 8X8 board of chess so that they cannot attack each other, that is, any two queens cannot be on the same row, column, or slash, and ask how many ways there are.

The N queen problem is the same.
At first glance, it looks like we're going to be using two dimensional arrays. Not really. One dimensional arrays, such as Arr[I], indicate that an element is in the I th row and the Arr[I] column - a widely used trick. And here we don't have to worry about storing the entire matrix, if Arr[I] exists, then when we print, when we print to the queen position, we print 1, and when we print to the non-queen position, we print 0.

There are a lot of ways to do this online, including the one mentioned earlier, so don't worry about whether you've improved or not, just as an exercise.

I'm going to go straight to the code, because I don't think there's much to say about recursive methods, and it's easy to understand if you think about space. I'll write about the non-recursive implementation tomorrow.



//The parameter rowCurrent represents the number of rows currently reached
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
bool Check(int rowCurrent,int *&NQueen);                         //Judging function
void Print(ofstream &os,int n,int *&NQueen);                                  //Print function
void Solve(int rowCurrent,int *&NQueen,int n,int &count, ofstream &os);           //N queen problem handler, index is generally 0

//Judging function , if there is a conflict, or if there is a conflict on the diagonal, return FALSE
bool Check(int rowCurrent,int *&NQueen)
{
    int i = 0;
    while(i < rowCurrent)
    {
        if(NQueen[i] == NQueen[rowCurrent] || (abs(NQueen[i]-NQueen[rowCurrent]) == abs(i-rowCurrent)) )
        {
            return false;
        }
        i++;
    }
    return true;
}
//Output all possible results to a text document
void Print(ofstream &os,int n,int *&NQueen)
{
    os<<" One call n";
    for (int i = 0;i < n;i++) {
        for(int j = 0 ; j < n; j++)
        {
            os<<(NQueen[i]==j?1:0);
            os<<setw(2);
        }
        os<<"n";
    }
    os<<"n";
}
//Core function. Recursively solve the N queen problem and print at the bottom
void Solve(int rowCurrent,int *&NQueen,int n,int &count, ofstream &os)
{
    if(rowCurrent == n)  //The number of current rows hits bottom, completes a matrix, outputs it
    {
        Print(os,n,NQueen);
        count++;
    }
    for(int i = 0;  i < n; i++)
    {
        NQueen[rowCurrent] = i;                     //Row I column try it
        if(Check(rowCurrent,NQueen))
        {
            Solve(rowCurrent+1,NQueen,n,count,os);  //Move down one row
        }
    }
}
int main()
{
    int n;           //Problem size
    int count = 0;   //Solution of the count
    cout<<" Please enter the size of the problem N"<<endl;
    cin>>n;
    if(n<4)
    {
        cerr<<"Problem size Must be greater than 4"<<endl;
        return 0;
    }
    int *NQueen = new int[n];
    ofstream os;
    os.open("result.txt");
    Solve(0,NQueen,n,count,os);
    cout<<" The solution to the problem is "<<count<<" methods "<<endl;
    os.close();
    return 0;
}


Related articles: