Luo Gu P1219 eight queens problem solution

Title Description

Checking a checkerboard below 6 x 6, and six pieces were placed on the board so that each row, column, and only one, each diagonal (including all the main diagonal two parallel lines) on at most one piece.

The above sequence can be layout 246 135 described, it denotes the i-th digit has a piece in the corresponding position of the i-th row, as follows:

Line No. 123 456

Column No. 246135

It's just checkers placed a solution. Please compile a program to find solutions to place all the checkers. Sequence and outputs them to the above method. Solutions lexicographically arranged. Please output of the previous three solutions. The last line is the total number of solutions.

// the following words from usaco official, does not represent the views Luo Valley

Special Note: For larger N (board size N x N) your program should be improved more effectively. Do not all solutions are computed in advance and then only output (or find a formula about it), this is cheating. If you insist on cheating, then you USACO Training login account deleted and can not participate in any race USACO of. I warned you!

Input Format

A number N (6 <= N <= 13) is an N x N represents the board size.

Output Format

Three solutions before the first three acts, separated by a space between two numbers each solution. The fourth line is only one number, the total number of Solutions.

Sample input and output

Input # 1
6
Output # 1
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

Description / Tips

Title Translation from NOCOW.

USACO Training Section 1.5


 

answer

This question is a standard DFS title. There is a very simple idea is to represent the grid after placing the pieces to be affected by a two-dimensional array vis. Each piece is placed a grid Hou all affected + 1, after the end of the lattice DFS -1.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>

using namespace std;

const int    MAXN = 1005;
int        n, s = 0, cnt = 0, cnt2 = 0;
int        vis[MAXN][MAXN], ans[MAXN][MAXN], map[MAXN][MAXN];

void dfs( int x )
{
    if ( x > n )
    {
        s++;
        cnt++;
        if ( cnt <= 3 )
        {
            cnt2 = 0;
            for ( int i = 1; i <= n; i++ )
            {
                for ( int j = 1; j <= n; j++ )
                {
                    if ( map[i][j] == 1 )
                    {
                        cnt2++;
                        ans[cnt][cnt2] = j;
                    }
                }
            }
        }
        return;
    }
    for ( int i = 1; i <= n; i++ )
    {
        if ( vis[x][i] == 0 )
        {
        //    cout << x << ", " << i << endl;
            vis[x][i]++;
            map[x][i] = 1;
            for ( int j = 1; j <= n; j++ )
            {
                vis[x][j]++;
                if ( j >= x )
                {
                    vis[j][i]++;
                }
                if ( x + j <= n && i >= j )
                {
                    vis[x + j][i - j]++;
                }
                if ( x + j <= n && i + j <= n )
                {
                    vis[x + j][i + j]++;
                }
            }
            dfs( x + 1 );
            vis[x][i]--;
            map[x][i] = 0;
            for ( int j = 1; j <= n; j++ )
            {
                vis[x][j]--;
                if ( j >= x )
                {
                    vis[j][i]--;
                }
                if ( x + j <= n && i >= j )
                {
                    vis[x + j][i - j]--;
                }
                if ( x + j <= n && i + j <= n )
                {
                    vis[x + j][i + j]--;
                }
            }
        }
    }
}


int main()
{
    cin >> n;
    dfs( 1 );
    for ( int i = 1; i <= 3; i++ )
    {
        for ( int j = 1; j <= n; j++ )
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    cout << cnt << endl;

    return(0);
}

Originally I thought that this code TLE, but it is fortunate that the AC codes. The last test point with a 800 + ms.

This code can be optimized, it may be a three-dimensional array instead of the two-dimensional array. A one-dimensional array representing all columns, as long as there is a piece of cloth in a column, the column corresponding to this array element is set to 1. Similar one-dimensional arrays representative of two lines and two diagonal lines parallel.

Guess you like

Origin www.cnblogs.com/zealsoft/p/11415432.html