Queen 2n of Blue Bridge cup, Queen 2n of Blue Bridge cup

Source: Internet
Author: User

Queen 2n of Blue Bridge cup, Queen 2n of Blue Bridge cup

Problem:Given an n * n Board, there are some positions in the Board that cannot be placed in the queen. Now we need to add n black queens and n WHITE queens to the chessboard so that any two black queens are not on the same row, column, or diagonal line.

The two white queens are not in the same row, the same column, or the same diagonal line. How many methods are there in total? N is less than or equal to 8.

Input Format

The first input behavior is an integer n, indicating the size of the Board.
In the next n rows, each line contains n integers 0 or 1. If an integer is 1, the corresponding position can be placed in the Queen. If an integer is 0, it indicates that the corresponding location cannot be placed in the queen.

Output Format

Output an integer to indicate the total number of placement methods.

Sample Input

4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

1 1 1 1

Sample output

2

Sample Input

4
1 0 1 1
1 1 1 1
1 1 1 1
1 1 1 1

Sample output

0

 

 

Analysis: 1.A two-dimensional array is used to construct an n * n matrix.

   2.Select a proper coordinate line by line to place the so-called black or white queen (if it is black, it will be white next time ).

3.Next, step 2, put another queen.

 

Steps 2 and 3:

1 void dfs (int i, int q) // i is the initial line q refers to whether to place the queen at a certain position
 2 {
 3 for (int j = 0; j <n; j ++)
 4 {
 5 // can't be put or already put
 6 if (s [i] [j] == 0 || s [i] [j] == 2) // 2 that has been let go
 7 {
 8 continue;
 9         }
10 int flag = 1; // Can be put by default
11 int y1 = j-1; // The black queen in the upper left corner
12 int y2 = j + 1; // The black queen in the upper right corner
13 for (int l = i-1; l> = 0; l--)
14 {
15 // Judge whether there are the same queen in the same column and diagonal line (the counterparts will definitely not have: from top to bottom)
16 // same column
17 if (s [l] [j] == q) // Check if there are the same queen on the same column
18 {
19 flag = 0;
20 break;
twenty one             }
22 // slash
23 if (y1> = 0 && s [l] [y1] == q) // Judge if there is on the left diagonal
twenty four             {
25 flag = 0;
26 break;
27}
28 y1--;
29 if (y2 <n && s [l] [y2] == q)
30 {
31 flag = 0;
32 break;
33}
34 y2 ++;
35}
36 if (flag)
37 {
38 s [i] [j] = q; // Queen
39 if (i <n-1)
40 {
41 dfs (i + 1, q);
42}
43 else
44 {
45 // The black queen is finished, start to put the white queen;
46 // After the white queen puts it, it is a way to end
47 if (q == 2)
48 {
49 dfs (0,3);
50}
51 else
52 {
53 count ++;
54}
55}
56 s [i] [j] = 1; // Resume to start next time
57}
58}
59}

Now we use C to implement

(In fact, I am not always using C ++)

1 #include <stdio.h>
 2 void dfs (int i, int q);
 3 int s [13] [13];
 4 int n, count = 0;
 5 int main () {
 6 scanf ("% d", & n);
 7 for (int i = 0; i <n; i ++)
 8     {
 9 for (int j = 0; j <n; j ++)
10 {
11 scanf ("% d", & s [i] [j]); // Assign a matrix
12}
13}
14 dfs (0,2); // Black Queen
15 printf ("% d", count);
16 return 0;
17}
18 void dfs (int i, int q) // i = 0 q = 2
19 {
20 for (int j = 0; j <n; j ++)
twenty one     {
22 // Can't be put or already put
23 if (s [i] [j] == 0 || s [i] [j] == 2) // It's already 2
twenty four         {
25 continue;
26}
27 int flag = 1; // Can be put by default
28 int y1 = j-1; // The black queen
29 int y2 = j + 1; // the black queen
30 for (int l = i-1; l> = 0; l--)
31 {
32 // Judge whether there is the same queen in the same column and diagonal line (the peer certainly won't have: from top to bottom)
33 // same column
34 if (s [l] [j] == q) // Judge if there are the same queen on the same column
35 {
36 flag = 0;
37 break;
38}
39 // slash
40 if (y1> = 0 && s [l] [y1] == q) // Judge if there is on the left diagonal
41 {
42 flag = 0;
43 break;
44}
45 y1--;
46 if (y2 <n && s [l] [y2] == q)
47 {
48 flag = 0;
49 break;
50}
51 y2 ++;
52}
53 if (flag)
54 {
55 s [i] [j] = q; // put the queen
56 if (i <n-1)
57 {
58 dfs (i + 1, q);
59}
60 else
61 {
62 // After the black queen is finished, start to put the white queen;
63 // After the white queen puts it, it is a way to end
64 if (q == 2)
65 {
66 dfs (0,3);
67}
68 else
69 {
70 count ++;
71}
72}
73 s [i] [j] = 1; // Resume to start next time
74}
75}
76}

The following is an unauthentic C ++

#include <iostream>
using namespace std;
int s [13] [13];
int n;
int count = 0; // count
void dfs (int i, int q) // i = 0 q = 2
{
    for (int j = 0; j <n; j ++)
    {
        // can't be put or already put
        if (s [i] [j] == 0 || s [i] [j] == 2) // It is 2
        {
            continue;
        }
        int flag = 1; // Can be put by default
        int y1 = j-1; // The black queen in the upper left corner
        int y2 = j + 1; // The black queen in the upper right corner
        for (int l = i-1; l> = 0; l--)
        {
            // Judge whether there are the same queen on the same column and slash (the counterparts will definitely not: from top to bottom)
            // same column
            if (s [l] [j] == q) // Check if there are the same queen on the same column
            {
                flag = 0;
                break;
            }
            // slash
            if (y1> = 0 && s [l] [y1] == q) // Judge if there is on the left diagonal
            {
                flag = 0;
                break;
            }
            y1--;
            if (y2 <n && s [l] [y2] == q)
            {
                flag = 0;
                break;
            }
            y2 ++;
        }
        if (flag)
        {
            s [i] [j] = q; // put the queen
            if (i <n-1)
            {
                dfs (i + 1, q);
            }
            else
            {
                // After the black queen is finished, start to put the white queen;
                // After the white queen puts it, it is a way to end
                if (q == 2)
                {
                    dfs (0,3);
                }
                else
                {
                    count ++;
                }
            }
            s [i] [j] = 1; // Resume to start next time
        }
    }
}
int main ()
{
    cin >> n; // assign n
    for (int i = 0; i <n; i ++)
    {
        for (int j = 0; j <n; j ++)
        {
            cin >> s [i] [j]; // Assign a matrix
        }
    }
    dfs (0,2); // Black Queen
    cout << count << endl;
    return 0;
} 

At the end, I am talking about myself, learning together, and making progress together. 1186294207


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.