Eight Queens, Queen N and their variants

Eight Queens Problem and N Queens Problem

The n-queen problem refers to placing n queens on a chess board of n∗n so that the queens cannot attack each other, that is, any two queens cannot be in the same row, column or diagonal line.

1_597ec77c49-8-queens.png

Now given an integer n, please output all the chess pieces that meet the conditions.

The input format is
one line and contains the integer n.

Output format
Each solution occupies n lines, and each line outputs a string of length n to represent the complete chessboard state.

Among them, "." means that the state of the square in a certain position is empty, and "Q" means that there is a queen on the square in a certain position.

After each program output is completed, a blank line is output.

The order of the output scheme is arbitrary, as long as it is not repeated or omitted.

Data range
1≤n≤9
input example:

4

Sample output:

.Q..
...Q
Q...
..Q.

..Q.
Q...
...Q
.Q..

C++ code:

#include<iostream>

using namespace std;

const int N = 10;

int n;
bool map[N][N];
bool col[N],Mdg[2*N],Ddg[2*N];//标记列,两条对角线是否存放棋子
//主对角线的下标用:col-line+n 标记,副对角线的下标用:col+line 标记

void dfs(int line)
{
    
    
    if(line > n) //表示放置完毕
    {
    
    
        for(int i = 1; i <= n; i ++)  //输出棋盘
        {
    
    
            for(int j = 1; j <= n; j ++)
            {
    
    
                if(map[i][j])
                    cout << "Q";
                else
                    cout << ".";
            }
            cout << endl;
        }
        cout << endl;
    }
    for(int i = 1; i <= n; i ++)
    {
    
    
        if(!col[i] && !Mdg[i - line + n] && !Ddg[i + line])
        {
    
    
            map[line][i] = true;
            col[i] = Mdg[i-line+n] = Ddg[i+line] = true;  //放置棋子后就需将列,主副对角线标记为true了
            dfs(line + 1);
            map[line][i] = col[i] = Mdg[i-line+n] = Ddg[i+line] = false; //状态回溯,重新标记为false
        }
    }
}

int main()
{
    
    
    cin >> n;
    dfs(1);
    return 0;
}

N queen variant problem

Problem description
  Given an n*n chessboard, there are some positions on the chessboard that cannot be placed on the queen. Now we need to put n black queens and n white queens on the chessboard, so that any two black queens are not in the same row, column or diagonal, and any two white queens are not in the same row, The same column or the same diagonal. How many ways are there in total? n is less than or equal to 8.
Input format
  The first line of input is an integer n, which represents the size of the chessboard.
  In the next n lines, there are n integers of 0 or 1 in each line. If an integer is 1, it means that the corresponding position can be a queen. If an integer is 0, it means that the corresponding position cannot be a queen.
Output format
  outputs an integer, indicating how many ways there are in total.
Sample input

4
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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;
int a[15][15];
int ans=0;
int n;
int location[20];

int valid(int row,int columns){
    
    
	for(int i = 1; i <= row - 1; i ++)
		if(columns==location[i] || abs(row-i) == abs(columns - location[i]))
			return 0;
	return 1;
}
	
void dfs(int line){
    
    
	if(row == n + 1){
    
    
		ans++;
		return;
	}
	for(int i=1;i<=n;i++){
    
    
		if(valid(line,i)){
    
    
			location[row]=i;
		 dfs(row+1);
    	}
	}
}//注意:return则只返回其中一种情况,否则输出返回所有情况;
int main() {
    
    

	scanf("%d",&n);
	
	dfs(1);
	printf("%d",ans);
	return 0;
}

Given an N×N chessboard, please place N pieces on it, and the requirements are as follows:

There is exactly one pawn in
each row and column. There can be at most one pawn on each diagonal.

    1   2   3   4   5   6
  -------------------------
1 |   | O |   |   |   |   |
  -------------------------
2 |   |   |   | O |   |   |
  -------------------------
3 |   |   |   |   |   | O |
  -------------------------
4 | O |   |   |   |   |   |
  -------------------------
5 |   |   | O |   |   |   |
  -------------------------
6 |   |   |   |   | O |   |
  -------------------------

The above figure shows a solution when N=6. The solution can be described by the sequence 2 4 6 1 3 5, which gives the sequence from the first row to the sixth row, and each row is placed The position of the column where the piece is located.

Please write a program, given an N×N chessboard and N chess pieces, please find out all the chess placement plans that meet the above conditions.

The input format is
one line, an integer N.

The output format
has four lines. Each of the first three lines outputs a sequence of integers to describe a feasible placement scheme. The i-th number in the sequence indicates the position of the column where the chess pieces in the i-th row should be placed.

The scheme described by these three lines should be the scheme with the integer sequence lexicographically ranked first, second, and third.

The fourth line outputs an integer that represents the total number of possible placement options.

Data range
6≤N≤13
input example:

6

Sample output:

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

C++ code

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 15;

int path[N];//记录棋子的位置路径
bool col[N],Mdg[2*N],Ddg[2*N];//标记列,两条对角线是否存放棋子
//主对角线的下标用:col-line+n 标记,副对角线的下标用:col+line 标记
int n,ans = 0;//ans记录总次数并输出前三次

void dfs(int line)// 行
{
    
    
    if(line > n) 
    {
    
    
        ans ++;
        if(ans <= 3)
        {
    
    
            for(int i = 1; i <= n; i ++)
                cout << path[i] << " ";
            cout << endl;
        }
        return;
    }
    for(int i = 1; i <= n; i ++)  //尝试在每列开始放置棋子
    {
    
    
        if(!col[i] && !Mdg[i-line+n] && !Ddg[i+line]) // 表示此时行,主副对角线均没有棋子放置。可以放置新棋子
        {
    
    
            path[line] = i;//在当前line行i列放置棋子
            col[i] = Mdg[i-line+n] = Ddg[i+line] = true;  //放置棋子后就需将列,主副对角线标记为true了
            dfs(line + 1);
            col[i] = Mdg[i-line+n] = Ddg[i+line] = false; //状态回溯,重新标记为false
        }
    }
}



int main()
{
    
    
    cin >> n;
    dfs(1);
    cout << ans << endl;
    return 0;
}

The chessboard problem Putting chess pieces
on a chessboard of a given shape (the shape may be irregular), there is no difference between the chess pieces.

It is required that any two chess pieces cannot be placed in the same row or column in the chessboard. Please program to solve the number C of all feasible placement schemes for k chess pieces for a chessboard of a given shape and size.

Input format The
input contains multiple sets of test data.

The first line of each group of data is two positive integers n, k, separated by a space, which means that the chessboard and the number of chess pieces will be described in a matrix of n∗n. When it is -1 -1, the input is over.

The subsequent n lines describe the shape of the chessboard: each line has n characters, where # represents the chessboard area, and. Represents the blank area (the data guarantees that there are no extra blank rows or blank columns).

Output format
For each group of data, one line of output is given, and the number of output solutions is C (data guarantee C<231).

Data range
n≤8, k≤n
input example:

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample output:

2
1
#include<iostream>
#include<cstring>
using namespace std;

const int N = 10;

char map[N][N];//存放棋盘
int n,k,ans = 0;
bool li[N],col[N];//标记列是否存放棋子
int res = 0;//放置棋子数


void dfs(int line)
{
    
    
    if(res == k)
    {
    
    
        ans ++;
        return;
    }
    if(line > n)   return;  //防止越界

    for(int i = 1; i <= n; i ++)
    {
    
    
        if(!col[i] && map[line][i] == '#')
        {
    
    
            col[i] = true;res ++;
            dfs(line + 1);
            col[i] = false;res --;
        }
    }
    dfs(line + 1);
    //这个是关键,因为 k <= line,因此可能有的行不用放棋子,所以我们要手动舍弃行,直接进入下一行 
}



int main()
{
    
    
    while(cin >> n >> k, n != -1 && k != -1)
    {
    
    
        res = 0;
        ans = 0;
        for(int i = 1; i <= n; i ++)
        {
    
    
            for(int j = 1; j <= n; j ++)
            {
    
    
                cin >> map[i][j];
            }
        }
        dfs(1);
        cout << ans << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/diviner_s/article/details/112997097