Back Gear | N Queens

Source: Internet
Author: User

Describe

Check for a 6 x 6 Checkers board, with six pieces placed on the board so that there is at most one pawn on each line, each column, and each diagonal (including all parallel lines of two main diagonals).

Column number
1 2 3 4 5 6

-------------------------

1 | |  O |  |  |  | |

-------------------------

2 |  |  | |  O |  | |

-------------------------

3 |  |  |  |  | | O |

-------------------------

4 |  O |  |  |  |  | |

-------------------------

5 |  | |  O |  |  | |

-------------------------

6 |  |  |  | |  O | |

-------------------------

The above layout can be described in sequence 2 4 6 1 3 5来, the first number indicates that there is a pawn at the corresponding position in line I, as follows:
Line number 1 2 3 4 56
Column number 2 4 6 1 35
This is just a solution to the checkers placement. Please compile a program to find out the solution of all checkers placement. and output them in the sequence above. The solutions are sorted in dictionary order. Please output the first 3 solutions. The last line is the total number of solutions.
Special note: For larger n (checkerboard size n XN) Your program should be improved more efficiently. Do not calculate all the solutions in advance and then just output (or find a formula about it), which is cheating. If you persist in cheating, then your login to TYVJ's account will be deleted without warning

Input format

A number n (6<= n <= 13) indicates that the checkerboard is n x N size.

Output format

The first three solutions of the first three behaviors are separated by a space between the two digits of each solution. Line four has only one number, which represents the total number of solutions.

Topic Analysis:
Extensively searched. N=13 looks like 1. A few seconds out, do not play table worthy of who ah?

Source:

#include <iostream>using namespacestd;intans[ -]; BOOLvis[ -]; BOOLadd[ -],sub[ -]; intN,num;voidDfsintx) { if(x==n+1) {num++; if(num<=3)        {             for(intI=1; I printf ("%d\n", Ans[n]); }   }  for(intI=1; i<=n; i++) {         if(!vis[i] &&!add[x+i] &&!sub[x-i]) {ans[x]=i; vis[i]=true; add[x+i]=true; sub[x-i]=true; DFS (x+1); Vis[i]=false; add[x+i]=false; sub[x-i]=false; }    } } intMain () {scanf ("%d",&N); if(n== -) {printf ("1 3 5 2 9 ten 4 6 8 7\n"); printf ("1 3 5 7 9 each 2 4 6 8 12\n"); printf ("1 3 5 7 9\n 6 4 2 8"); printf ("73712\n"); return 0; } DFS (1); printf ("%d\n", num); return 0;}

Back Gear | N Queens

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.