Blue Bridge Cup basic exercises 2n Queens

Blue Bridge Cup basic exercises 2n Queens

Problem Description
  Given an n * n board, the board can not be put in the position of some of the Queen. To now black board placed n and n-queens White Queen, so that any two black queens are not on the same line, same column or the same diagonal line, any two White Queen not in the same row, the same column or the same diagonal. Q. A total of how many put the law? n is 8 or less.
Input format
  input acts a first integer n, the size of the board.
  Subsequently n rows, each row of n an integer of 0 or 1, if a is an integer of 1, indicating the position corresponding to queen can be placed, if a is an integer of 0, indicating the position corresponding to the discharge can not be queen.
Output format
  output an integer representing the total number of species put law.
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

Outline of Solution
Before this problem solution, need to understand the n-queens problem, reference may hereinafter blog
N Queens
next question describes the main idea
main difference between n and 2n Queen queens problem is that, in the 2n queens problem, it is necessary wherein the placement of one color record Queen, after placing, when the queen and put another color has been placed in the bypass position.
I use a depth-first search method, first Queen black display, record the display position, the visit [i] [j] array is set to 1 (i, j is a cross-ordinate) are placed black Queen bled off, start placing the white Queen, the same principle, only to ensure that when placed from Visit [i] [J] == 0 can be.
Specific code as follows

The code (C ++)

#include<iostream>
#include<cmath>
#define maxsize 8
using namespace std;
int n;
int num=0;//输出结果 符合条件的摆法
int res_b[maxsize]; //符合条件的黑皇后摆放序列
int res_w[maxsize];//符合条件的白皇后摆放序列
int map[maxsize][maxsize]; 
int visit[maxsize][maxsize];//记录
int dfs_white(int step);
int dfs_black(int step);
bool check_b(int step);
bool check_w(int step);
int main()
{
	cin>>n;

	for(int i=0;i<n;i++)
	  for(int j=0;j<n;j++)
	  {
	  	cin>>map[i][j];
	  	visit[i][j]=0;
	  }
	dfs_black(1); 
	cout<<num; 
	return 0;
	
}
int dfs_white(int step)
{
 if(step==n+1)//白皇后全部摆放完成
  {
  	num++;
  }	
  for(int i=0;i<n;i++)
  {
  	if(!map[i][step-1]||visit[i][step-1]==1) //是否能存放皇后 
  	continue;
  	res_w[step-1]=i;
	if(check_w(step))
	{
		visit[i][step-1]=1;
		dfs_white(step+1);
		visit[i][step-1]=0;
	}
  	
  }	
}
int dfs_black(int step)
{
  if(step==n+1)
  {
  	dfs_white(1);//黑皇后全部摆放完成,开始统计白皇后
  }	
  for(int i=0;i<n;i++)
  {
  	if(!map[i][step-1]) //是否能存放皇后 
  	continue;
  	res_b[step-1]=i;
	if(check_b(step))
	{
		visit[i][step-1]=1;
		dfs_black(step+1);
		visit[i][step-1]=0;
	}
  	
  }
}

bool check_b(int step)
{
	for(int i=0;i<step-1;i++)
	{
		if(res_b[i]==res_b[step-1]||abs(step-1-i)==abs(res_b[step-1]-res_b[i])) //保证摆放的位置之前没有出现过 和不在对角线上
		return false;
	}
	return true;
}
bool check_w(int step)
{
	for(int i=0;i<step-1;i++)
	{
		if(res_w[i]==res_w[step-1]||abs(step-1-i)==abs(res_w[step-1]-res_w[i]))//保证摆放的位置之前没有出现过 和不在对角线上
		return false;
	}
	return true;
}

(There is a pit in the Blue Bridge cup of oj, the sixth point I have not but my output by 2406 is right)
just started writing blog, there is a problem please advise

Released eight original articles · won praise 0 · Views 709

Guess you like

Origin blog.csdn.net/Daturasee/article/details/104784543