N queen question, n queen

Source: Internet
Author: User

N queen question, n queen
Description

Place n queens that are not under attack on the chessboard of n × n grids. According to the rules of chess, the queen can attack pawns in the same row, column, or diagonal line. The problem after n is equivalent to placing n on the n × n board. Any two queens may wish to be on the same row, column, or diagonal line.

Input/Output Format

Input description:

Given the size of the Board n (n ≤ 13)

Output description:

The output integer indicates the number of placement methods.

Input and Output sample

Input example #1:

8

Output sample #1:

92

Ideas

Search.

Code # include <stdio. h> int a [200] = {0}, B [200] = {0}, c [200] = {0}, d [200], p = 0; int search (int n, int s) {int I; for (I = 1; I <= n; I ++) {if (a [I] = 0 & B [I + s] = 0 & c [s-I + n-1] = 0) {a [I] = 1; B [I + s] = 1; c [s-I + n-1] = 1; d [s] = I; if (s = n) p ++; else search (n, s + 1); a [I] = 0; B [I + s] = 0; c [s-I + n-1] = 0 ;}} int main () {int n; scanf ("% d", & n); search (n, 1 ); if (p = 0) printf ("no solute! "); Else printf (" % d ", p); return 0;} View Code

 

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.