12,198 Queens

Source: Internet
Author: User

Difficulty: popularity/improvement-

title type: Deep Search

Number of submissions: 2

related knowledge: deep Search

Title Description

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

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 5 6

Column number 2 4 6 1 3 5

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.

The following words are from the official Usaco, do not represent the rokua point of view

Special note: For larger n (checkerboard size n x N) 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 you log in to Usaco Training's account deleted and cannot participate in any contest of Usaco. I warned you!

Input/output format

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.

Code:

1#include <iostream>2#include <cstring>3 using namespacestd;4 intc[ -];5 BOOLvisit[3][ -];6 intans;7 intN;8 voidprint () {9     inti;Ten      for(i =1; I <= N; i++) Onecout<<c[i]<<" "; Acout<<Endl; - } - voidDfsintcur) { the     intI, J; -     if(cur==n+1){ -ans++; -         if(ans<=3) + print (); -     } +     Else{ A          for(i =1; I <= N; i++){ at             if(!visit[0][i]&&!visit[1][cur+i]&&!visit[2][cur-i+ N]) { -C[cur] =i; -visit[0][i] = visit[1][cur+i] = visit[2][cur-i+n] =true; -DFS (cur+1); -visit[0][i] = visit[1][cur+i] = visit[2][cur-i+n] =false; -             } in         } -     }  to }  + intMain () { -Cin>>N; theDfs1); *cout<<ans; $     return 0;Panax Notoginseng}

Note:

The classic topic, seems to have written several times, the result is still not able to write completely independently, referring to the code on the PPT. I actually thought I had almost mastered Dfs QwQ. In addition, this problem does not add visit array to optimize the last point is too much! But I think I added the visit. The array structure is clearer and more in line with the DFS framework.

12,198 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.