Using recursion to find the problem of eight queens

Source: Internet
Author: User

Eight Queen's question is a western chess player called Marx in 1848, and then the great mathematician Mr. Gauss worked out 76 kinds of hardships, then the computer was not born so even the accepted mathematical master Gauss can not be accurately calculated, the correct result is 92 kinds. The eight Queens question is the study data structure is a very classic problem, in the 8*8 board, we all know that the Queen is horizontal, vertical, oblique can move, and the distance is unlimited. Place 8 queens so that they do not pose a threat between 22 and how they are placed.


This picture is looking for Niang want, even if still do not understand the person according to this picture can also know the structure of the problem:



Purpose of calculation:

1, traverse and output each kind of result;

2. How many kinds of solutions are obtained.


Using backtracking algorithm to calculate the Queen's problem is relatively simple. I use recursion, just to solve. is to get a deeper understanding of recursion.


Enter the code, I will make detailed comments on the code, to facilitate the needs of students to learn:


Start from main:

#include <stdio.h>
#include <stdlib.h>

#define N 8/			* range of arrays, representing 8*8 's checkerboard */
	
int count=0;			/* Used to count how many algorithms, SOLID global Variables *

/int main (void)
{
	int chess[n][n];

	for (int i=0;i<n;i++)/		* Initialize the chessboard, 0 for empty, 1 for Queen *
	/{for
		(int j=0;j<n;j++)
		{
			chess[i][j]=0;
		}
	}

	Queen (0,n,chess);		/* Enter recursive *
	/System ("pause");
	return 0;
}


The recursive function is implemented as follows:

The first parameter is to be clear, starting with the number of lines, not the total number of lines

void Queen (int row,int col, int (*chess) [8])	
{
	int tempchess[n][n],i,j;		/* Create a temporary chessboard for output *
	/for (i=0;i<n;i++)
	{for
		(j=0;j<n;j++)
		{
			tempchess[i][j]=chess[i][j];		/* Initialize the TEMPORARY board
		*
	/}}

	if (8==row)/					* Recursive end condition, when the 8th line is completed, the recursion ends *
		/{printf ("%d case. \ n", Count ++);		/* Output checkerboard and count++*/for
		(i=0;i<n;i++)			
		{
			for (j=0;j<n;j++)
			{
				printf ("%d", tempchess[i ][J]);
			}
			printf ("\ n");
		}
		printf ("\ n");
	}
	else
	{/					* core recursion Zone */For
		(j=0;j<col;i++)		
		{
			if (Notdanger (row,j,tempchess))/		* Use a function to determine if the location is safe, if safe enters the recursive condition *
			/{for
				(i=0;i<n;i++)
				{
					tempchess[row][i]=0;	/* First populate the column with 0*/
				}
				chess2[row][j]=1;		/* Place Queen *
				/Queen (row+1,col,tempchess) in the corresponding position;	/* Recursive call, here row++ only. */
			}
		}
	}
}



Write well, in fact, so simple, a few lines just. But beginners can be more difficult to understand. Of course, don't forget to implement the Notdanger method:

int Notdanger (int row,int col,int (*chess) [N])
{for


	(int i=row-1;i>=0;i--)
	{
		if (chess[i][col]==1 )
		{
			return 0;
		}
	}


	for (int i=row+1;i<n;i++)
	{
		if (chess[i][col]==1)
		{
			return 0;
		}
	}


	for (int i=row-1,j=col-1;i>=0&&j>=0;i--, j--)
	{
		if (chess[i][j]==1)
		{
			return 0;
		}
	}


	for (int i=row+1,j=col+1;i<n && j<n;i++,j++)
	{
		if (chess[i][j]==1)
		{
			return 0;
		}
	}


	for (int i=row-1,j=col+1;i>=0 && j<n;i--, j + +)
	{
		if (chess[i][j]==1)
		{
			return 0;
		}
	}


	for (int i=row+1,j=col-1;i<n&&j>=0;i++,j--)
	{
		if (chess[i][j]==1)
		{
			return 0;
		}
	}


	return 1;
}



Look at the results of the output:


In fact, there are many, if the output of the file can be output.

All right, it's over. I really think the eight Queens question is not a simple question, especially for me. In order to understand this algorithm I also spent a lot of time to understand ... The data structure is like this, must persevere.

Come on...

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.