Algorithm Training King, post legend (eight queen question)

Source: Internet
Author: User
Problem description The Earth people know, in chess, like the sun, radiant, awe-imposing eight sides, it can control the position of the horizontal, firm, Slash.
The Chinese people who have seen the Qing Palace play know that the harem is a dangerous place to walk. The Queens have their own sphere of influence, but they can always find a way to be peaceful.
All the Chinese know that the imperial power is sacred, accompanied by the tiger, the Touch Longyan die ...
Now there is a N*n palace, where the king occupies a total of 9 squares in his place and around them, and these lattice queens cannot be used (if the king is on the side of the palace, it may occupy less than 9 squares). Of course, the Queen will not attack the king.
Now that the king's position is known (x, y) (The king is located in row x, column y, the starting line of X, Y, and column 1), how many options are there to place n queens so that they cannot attack each other. Input format one line, three integers, the size of the imperial palace and the location of the king represents an integer representing the number of cases in which n queens are placed, sample input 8 2 2 sample output 10 data size and conventions

N<=12

Ideas:

8 Queen problem, more a king's position (the king's upper and lower left and right bottom left and bottom right) can not stand queen, the others are the same

Code:

#include <iostream>
#include <algorithm>
using namespace std;
int a[13]={0},cnt=0,n,x,y;
int check (int i)
{
	int j,k;
	for (j=1;j<i;j++)
	{
		if (A[i]==a[j])//The same column has a value of 
		 return 0;
		else if (a[i]-a[j]==i-j)//left diagonal condition 
		 return 0;
		else if (a[i]-a[j]==-(I-J))//Right diagonal condition 
		 return 0;
	}
	if ((i==x-1&&a[i]==y-1) | | | (i==x-1&&a[i]==y) | | (i==x-1&&a[i]==y+1) 
	| | (i==x&&a[i]==y-1) | | (i==x&&a[i]==y) | | (i==x&&a[i]==y+1)
	| | (i==x+1&&a[i]==y-1) | | (i==x+1&&a[i]==y) | | (i==x+1&&a[i]==y+1)) Cannot return 0 in the King's 9 positions
	 ;
	return 1;
}
void Dfs (int s)
{
	int i;
	if (s>n)//Find 1 kinds of solution 
	{
		cnt++;
		return;
	}
	for (i=1;i<=n;i++)
	{
		if (s==x&&i==y)
		 continue;
		a[s]=i;
		if (check (s))//Determine if Queen 
		{
			dfs (s+1) can be placed;//Next line
		}}
}
int main ()
{
	cin> >n>>x>>y;
	DFS (1); The king's position is from the beginning of the 1 
	cout<<cnt<<endl;
	return 0;
}



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.