Implement N queen recursively

Source: Internet
Author: User

In fact, it was a bit interesting to see a blogger who wrote a note on the subject yesterday, the day before yesterday. He wanted to write it on his own.

In fact, I found that the above student wrote

 

Place eight queens on chess so that they cannot attack each other. That is to say, neither of the two queens can be in the same row, column, or diagonal line.

 

Extend

 

A two-dimensional array seems to be used. Not required. One-dimensional array, for example

 

There are a lot of ways to implement this idea on the Internet, including those mentioned above, so don't worry about whether there are any improvements or not. You can just exercise it once.

The code is good. I think there is nothing to say about recursive methods. It is easy to understand the space. Write non-recursive implementations when you are free tomorrow.

#include<iostream><fstream><iomanip><stdlib.h>  Check( rowCurrent, *&NQueen);                          Print(ofstream &os, n, *&NQueen);                                   Solve( rowCurrent, *&NQueen, n, &count, ofstream &os);            Check( rowCurrent, *& i = (i <(NQueen[i] == NQueen[rowCurrent] || (abs(NQueen[i]-NQueen[rowCurrent]) == abs(i- ++  Print(ofstream &os, n, *&<< ( i = ;i < n;i++( j =  ; j < n; j++<<(NQueen[i]==j?:<<setw(<<<< Solve( rowCurrent, *&NQueen, n, &count, ofstream &(rowCurrent == n)  ++( i = ;  i < n; i++= i;                             +,NQueen,n,count,os);   n;                count = ;       cout<<<<>>(n<<<<<  *NQueen =  <<<<count<<<< 

By the way, we will give the casual address of the student mentioned above:

Http://www.cnblogs.com/FZQL/p/3485616.html

 

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.