DFS personal understanding and test examples

Depth First Search (DFS):

        is a means of searching. It can be understood as: it starts from a certain position (starting point), keeps going forward along a road until the end, then takes a step back and takes other paths that have not been taken, if not, take another step back and go again Select until the destination (end point) is found.

For example the following picture:

Start from A (starting point), go ABD first

Find no child node at D, push it back to node B, and go to EG

When node G finds that it has come to an end again, then step back to node E and find that node E has no right node

Then go back to node B and find that the left and right nodes of B have passed, then go back to node A and go to AC

Node C has no left subtree, go to the right subtree F, F is the end, retreat to C, retreat to A, and return.


Applicable scene:

There are three aspects, namely input data, state transition diagram, and solution target;

Input data : If it is a recursive data structure, such as a singly linked list, a binary tree, and a set, deep search can be used 100%; if it is a non-recursive data structure, such as one-dimensional arrays, two-dimensional arrays, strings, and graphs, the probability is smaller;

State transition diagram : tree or graph;

Input data : You must go to the deepest point (for example, for a tree, you must go to the leaf node) to get a solution, which is more suitable for deep search;


Example 1:

Given integers a1,a2.....an, determine whether a number of numbers can be selected such that their sum is exactly k.

(Constraints: 1<=n<=20, - 10 8 <= 10 8 , - 10 8 <=k<= 10 8 )


Simple DFS application, each data has two choices: add or not add, time complexity: 2 n .

Code:

 
 
#define MAX_N 1005
int data[MAX_N];
int n,k;
// sum from previous i items
bool DFS(int i,int sum)
{
	// All n items have been calculated to determine whether it is equal to k
	if(i == n)
		return sum == k;
	// don't add item i
	if(DFS(i+1,sum))
		return true;
	// add item i
	if(DFS(i+1,sum+data[i]))
		return true;
	
	return false;
}

result:


        Well, a simple example, even if it is a competition, it is usually the top 3 questions in the freshman year.

Example 2:

        There is a yard of size * M that has accumulated water after the rain. 'W' means standing water, '.' means no standing water. Eight connected ponds are considered to be connected together. How many puddles are there in total in the yard? (Constraints: N,M<=100)

(Eight connected means: upper, lower, left, right, upper left, lower left, upper right, lower right are all directly connected. The following figure is relative to the * part of W )

* * *

*W*

* * *


Code:

 
 
#define MAX_N 100

int N,M;
int Map[100][100];

void DFS(int x,int y)
{
	Map[x][y] = '.'; // The judged position becomes '.' and there is no stagnant water
	// Determine the eight connected positions
	for(int dx=-1;dx<=1;dx++)
		for (int dy = -1; dy <= 1; dy ++)
		{
			int Nx = x + dx;
			int Ny = y + dy;
			if(Map[Nx][Ny] == 'W' && Nx>=0 && Nx<N && Ny>=0 && Ny<M)
				DFS (Nx, Ny);
		}
	return ;
}
int Solve()
{
	int Years = 0;
	for(int i=0;i<N;i++)
		for(int j=0;j<M;j++)
		{
			if(Map[i][j] == 'W')
			{
				// Every time you enter this judgment, it means a new puddle is added, and all the 'W' in the puddle are changed to '.'
				Years++;
				DFS(i,j);
			}
		}
	return Ans;
}
result:


Test data:

10 12
W........WW.
.WWW ..... WWW
.... WW ... WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
I will update BFS in a while. I am a little confused by dynamic programming recently, and it may be later.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324517556&siteId=291194637