The depth first search of C language learning

Source: Internet
Author: User

See old iron often use DFS, has not known what is DFS, hurriedly remedial remedial lessons. Then I wrote this blog to study depth first search.

I refer to the "Aha, Algorithm," a book, read can be skipped, have not seen the can with me ha.

First of all, a case in question:

If there are 3 cards numbered 1, 2, 3 and 1 boxes numbered 2, 3 and 3. Now you need to put these 3 cards in 3 boxes, and each box has only one playing card. How many different methods are there?

The core of Dfs is "What to do Now". below, I will be based on the learning method from the beginning to start writing.

The first is the process of putting cards into the box:

for (i=0;i<n;i++)
{
	a[step]=i;
}
Parse: A is a box, and I put it into the first step box.

Here's a box marked with cards that have been spared:

for (i=1;i<n;i++)
{
	if (book[i]==0)
	{
		a[step]=i;
		Book[i]=1
	}
}
Book is whether the cards in the hands, 1 is not in the hands, 0 in the hands.

Note that we have finished processing the first step box, and as I said earlier, the core of Dfs is what to do now, which means that the DFS function is basically done.

void Dfs (int step)
{for
(i=0;i<n;i++)
{
if (book[i]==0)
{
a[            step]=i;
Book[i]=1;
}
return
;
Here may be a good understanding, although the book said it is easy to think of, but I did not immediately understand, varies from person to person.

We've finished with step one, so we should deal with the Step+1 box:

void Dfs (int step)
{for
	(i=0;i<n;i++)
	{
		if (book[i]==0)
		{
			a[step]=i;
			Book[i]=1;
			DFS (step+1);//through the function of the recursive processing step+1 
			book[i]=0;//Here is very important ... Take back the poker you just tried in order to proceed to the next step}
Yes, as the recursive, did not learn the recursion of their own science.

Next is the print order:

void Dfs (int step)
{
	if (step==n+1)
	{
		//) outputs an arrangement (the poker number in the 1-n box) for
		(i=0;i<n;i++) 
			printf ( "%d", A[i]);
		printf ("\ n");
		
		return;//returns to the previous step, where the DFS function was last invoked 
	} for 
	(i=0;i<n;i++)
	{
		if (book[i]==0)
		{
			A[step] =i;
			Book[i]=1;
			DFS (step+1);//through the function of the recursive processing step+1 
			book[i]=0;//Here is very important ... Take back the poker you just tried in order to proceed to the next step}
Next is the complete code:

#include <stdio.h>
int a[10],book[10],n;//Define the required global variable 
void dfs (int step)
{
	int i;
	if (step==n+1)
	{
		//Output an arrangement (card number in the 1-n box) for
		(i=1;i<=n;i++) 
			printf ("%d", a[i));
		printf ("\ n");
		
		return;//returns to the previous step, where the DFS function was last invoked 
	} for 
	(i=1;i<=n;i++)
	{
		if (book[i]==0)
		{
			a[ step]=i;
			Book[i]=1;
			DFS (step+1);//through the function of the recursive processing step+1 
			book[i]=0;//Here is very important ... Take back the poker you just tried in order to proceed to the next step}
int main () {
	scanf ("%d", &n);
	DFS (1);
	return 0; 
} 

Very concise code, but the function is very powerful.

Input: 3

Output:

123
132
213
231
312
321

The basic template for Dfs was later written:

void Dfs (int step)
{uses the
	if judgment boundary
	to attempt each possible for (i=1;i<=n;i++)
	{
		using recursive DFS (step+1); 
	}
	return;
}

This is the DFS idea, say so much, what is DFS in the end.

Answer: Depth Search

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.