C + + recursive implementation n Queens Problem code (eight Queens problem) _c language

Source: Internet
Author: User
Tags abs

Let's take a look at the basics of the 8 Queens question:

Put eight queens on 8x8 's chess, so that they can't attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum method.

Extending to n Queens is the same problem.
Look, it seems to be using a two-dimensional array. Actually, it's not necessary. One-dimensional arrays can be judged, such as arr[i], to represent an element in row I arr[i] column--a wide range of tips. And here we do not have to consider to store the entire matrix, if Arr[i] exists, then we print, print to the Queen position when the output of 1, non-queen bit output 0 can be.

This way of thinking of the realization of a lot of online, including the previous mention of the classmate, so also do not have to struggle to improve there is no improvement and so on, the right to be a practice.

Directly on the code well, feel that the recursive method is nothing to say, space to think of the ability to better understand a little bit easily. Tomorrow is free to write and write not recursive implementation bar.

Copy Code code as follows:

/*
* NQueen.cpp
*
* Created on:2013 Year December 23
* Author:nerohwang
*/
The formal parameter rowcurrent represents the number of rows currently in the
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace Std;
BOOL Check (int rowcurrent,int *&nqueen); Judgment 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 handling function, index general initial value is 0


//Judgment function, either there is a conflict at all, or there is a conflict on the slash, 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 text document for all possible results
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 functions. Recursively solves the N-Queens problem and prints the bottom of the
void Solve (int rowcurrent,int *&nqueen,int n,int &count, ofstream &os)
{
    if (rowcurrent = n)  //Current line number bottom, that is, complete a matrix, output it
    {
         Print (Os,n,nqueen);
        count++;
   }
    for (int i = 0;  i < n; i++)
    {
     & nbsp;  Nqueen[rowcurrent] = i;                     //row Row I column try
        if check ( Rowcurrent,nqueen))
        {
             Solve (Rowcurrent+1,nqueen,n,count,os); //Move Down line
        }
   }

int main ()
{
int n; Scale of problems
int count = 0; Count of Solutions
cout<< "Please enter the size of the problem n" <<endl;
cin>>n;
if (n<4)
{
cerr<< "problem scale 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 of the problem is" <<count<< "Way" <<endl;
Os.close ();
return 0;
}

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.