P1219 Queen eight, p1219 queen

Source: Internet
Author: User

P1219 Queen eight, p1219 queen
Description

Check a 6x6 checkers board, where six pawns are placed on the board, so that each row and column have only one, each diagonal line (including all parallel lines of two main diagonal lines) has at most one pawn.

The above layout can be described using sequence 2 4 6 1 3 5. the number I represents a piece at the corresponding position of line I, as shown below:

Row number 1 2 3 4 5 6

Column Number 2 4 6 1 3 5

This is just a solution for checkers. Compile a program to find the solutions for all checkers. And output the sequence methods above. The solution is ordered in alphabetical order. Output the first three solutions. The last row is the total number of solutions.

// The following is from the usaco official website and does not represent the opinion of logu.

Note: For a larger N (board size N x N), your program should be improved to be more effective. Do not calculate all the solutions in advance and only output (or find a formula about it). This is cheating. If you insist on cheating, you can log on to the USACO Training account to delete it and cannot participate in any USACO competitions. I warned you!

Input/Output Format Input Format:

A number N (6 <= N <= 13) indicates that the board is N x N.

Output Format:

The first three actions are the first three solutions. The two numbers of each solution are separated by a space. The fourth row has only one number, indicating the total number of solutions.

Input and Output sample Input example #1:
6
Output sample #1:
2 4 6 1 3 53 6 2 5 1 44 1 5 2 6 34
Description

The question translation is from NOCOW.

USACO Training Section 1.5

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int n; 7 int hang[150]; 8 int lie[150]; 9 int zdj[150];10 int ydj[150];11 int tot=0;12 int ans[150];13 int num=0;14 void dfs(int x)15 {16     if(x==n+1)17     {18         if(tot<3)19         {20             for(int i=1;i<=n;i++)21                 printf("%d ",ans[i]);22             printf("\n");23         }24         tot++;25     }26     for(int y=1;y<=n;y++)27     {28         if(lie[y]==0&&zdj[x-y+n]==0&&ydj[x+y]==0)29         {30             ans[++num]=y;31             lie[y]=1;zdj[x-y+n]=1;ydj[x+y]=1;32             dfs(x+1);33             ans[num--]=0;34             lie[y]=0;zdj[x-y+n]=0;ydj[x+y]=0;35         }36     }37     38     39 }40 int main()41 {42     43     scanf("%d",&n);44     dfs(1);45     cout<<tot;46     return 0;47 }

 

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.