[Learn with Hao] Deep Search (DFS)

Source: Internet
Author: User

* ************************* For reprinting, please indicate the source! ******************************

What is search

First, learning to search, we must know what we want to search. Abstract: It is a certain state. In addition, the search is achieved through the transfer between various States. For example, we walk through the maze. You can go to a certain position in the maze through a certain path and view it as a State. If you want to find a way out of the maze, you can see that starting from the entrance point, you can move it step by step until you appear at the exit position.

 

Backtracking Algorithm

Backtracking is also called testing. First, stick it to Baidu:

"General steps to solve the problem using the backtracking algorithm:

1. Define the solution space for the given problem. It contains at least one (optimal) solution of the problem.

2. Determine the space structure that is easy to search, so that you can use the Backtracking Method to easily search for the entire space.

3. Search for the space in depth-first mode, and use the pruning function during the search process to avoid invalid search.

The space for solving a problem is usually generated dynamically in the process of searching for a problem, which is an important part of the backtracking algorithm.

Feature ."

We have to make it easy to understand. Let's take an example: we can traverse the jiugong ge from the upper left corner. Each time we can only go one grid (up and down), and we can't go any more. (Emma, just after writing the program, shows the tracing process in an image with many details .) Total number of output steps. There are eight types of results I have written, and there is a dedicated log in the home page that contains all the status diagrams traversed by the nine cells. The image shows you how the status is directly converted. You must take a closer look. I will post the code for backtracking below:

// The arrays XX and YY which frequently occur in the maze, let's think about what this is,

// Do not know how to add group question: 80103249

Int XX [5] = {0,-1, 0, 1, 0 };

Int YY [5] = {0, 0, 1, 0,-1 };

// The following is the Backtracking part of the DFS () function.

For (INT I = 1; I <= 4; I ++)

{

Int XT = x + XX [I]; // here XX and YY appear. Let's take a look at the for loop and these two arrays.

Int yt = Y + YY [I]; // What is the role.

 

If (inpan (XT, yt) & Pan [XT] [yt]) // inpan () determines whether it is in the jiugongge. Pan [XT] [yt] determines whether it is

{// No.

Pan [XT] [yt] = 0; // if you can go, set it to zero.

DFS (XT, yt); // continue Deep Search

Pan [XT] [yt] = 1; // This is a back-to-back process.

}

}

The for loop is used to take four types of decisions-Up, down, and left. When a decision is adopted, we set the corresponding grid to zero and call DFS () to continue searching from that grid. However, when DFS () is over, we need to proceed with the next decision. At this time, we need to restore the site to its original state, that is, it seems that JIU gongge did not make the last decision. The method is to set the point that has just been set to zero and then back to 1. Because of the existence of backtracking, after the DFS () Exit, it will not change the appearance of the jiugongge. It may be difficult to look back and understand, but it is still old saying that once you understand it, you will find that it is a natural thing!

 

Deep Search

Finally, let's talk about deep search. Deep Search DFS, deep first search ). As we have said before, search is in various States. These statuses can form a state tree. There is a first state. Through some decisions, several States can be generated, which can be seen as a child node in the previous state. During Deep Search, you only need to save one status at a time. Compared with wide search, the memory overhead is smaller, and programming implementation is relatively easy. I don't know if there are any other advantages.

 

For the time being, deep search is not very informative, because it is really hard to explain. The main part is the chestnut ~ The most important thing is that you practice more, think more, and communicate with others.

 

PS: The program (. cpp) that JIU Gong Ge traverses. If you are interested, you can add the QQ group: 80103249, which is already shared.

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.