Search entry-DFS-Analysis of classic maze Problems

Source: Internet
Author: User

Today, let's talk about the introduction to DFS. I have seen the introduction to DFS before. Is that really an introduction? I 've posted the implementation steps of DFS. After reading this, I know the steps of DFS, but there is no concept for code implementation. Today, I want to write some of my own experiences. It's really literal-getting started.

 

DFS, that is, deep-Priority Search, is a search policy that searches for as deep as possible at the end of each search and searches for other nodes. This is a strategy of "not hitting the south wall, not turning back.

 

Actually, traversal is not as intuitive as the traversal of a linear array. Speaking of backtracking, every time I see this word, I have to think about it for a while. How is stack used for backtracking? Today, we actually performed BFs to find out that this was done by the compiler (as long as it is written in recursive form )... of course, non-recursive DFS implementation must be implemented on its own...

 

 

Maze Problems

The Maze problem is a classic problem of getting started with DFS. Here we will talk about how to use DFS with hdoj 1010 tempter of bone.

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010

Theme:

S.x.

. X.

.. XD

....

A "Maze" composed of the above four symbols is given ",'. 'indicates a block that can pass through, 'x' indicates that the wall cannot pass through,' s' indicates the start point, and 'D' indicates the end point, every second is required and only one step (top, bottom, left, right) can be taken to determine whether it can arrive at the end of the T Second. The '.' block that passes through each step disappears immediately and cannot be taken any more.

 

Of course, the algorithm is DFS, which is actually a brute force enumeration. Each step is judged by four branches, and some pruning is added, remove some results that obviously do not match the meaning of the question to meet the requirements of time.

Paste the code first:

1 # include <cstdlib> 2 # include <cstring> 3 # include <iostream> 4 using namespace STD; 5 char map [9] [9]; // input the maze Matrix 6 int dir [4] [2] = {1, 0}, {0, 1}, {-1, 0}, {0, -1 }}; // Four Directions: 7 int OK = 0; 8 int n, m, T, Si, SJ, Di, DJ; 9 int DFS (INT Si, int SJ, int CNT) 10 {11 if (SI <= 0 | SJ <= 0 | Si> N | SJ> m) // if the path exceeds the boundary, the system returns 12 {13 return 0; 14} 15 if (SI = di & SJ = Dj & CNT = T) // if the end point is found, the flag position is 116 {17 OK = 1; 18} 19 if (OK) 20 {21 return 1; 22} 23 int temp = T-CNT-ABS (di-Si) -Abs (Dj-SJ); // here is the pruning. I didn't write it here, and time limit exceeded more than a dozen times... next we will discuss 24 if (temp <0 | temp & 1) 25 {26 return 0; 27} 28 for (INT I = 0; I <4; ++ I) // explore each node in four directions 29 {30 if (Map [Si + dir [I] [0] [SJ + dir [I] [1] ]! = 'X') 31 {32 map [Si + dir [I] [0] [SJ + dir [I] [1] = 'X '; // if the path you have traveled cannot be taken, set it to wall 33 DFS (Si + dir [I] [0], SJ + dir [I] [1], CNT + 1); 34 map [Si + dir [I] [0] [SJ + dir [I] [1] = '. '; // when exploring the next path, the node should be restored to the available status 35} 36} 37 return 0; 38} 39 int main () 40 {41 while (CIN> N> m> T) 42 {43 int wall = 0; 44 OK = 0; 45 if (n = 0 & M = 0 & t = 0) 46 {47 break; 48} 49 for (INT I = 1; I <= N; ++ I) 50 {51 for (Int J = 1; j <= m; ++ J) 52 {53 CIN> map [I] [J]; // scanf ("% C", & map [I] [J]), which is also written here; it's silly not to talk about it... however, scanf ("% s", & map [I]) can be written in the for (I) loop in this way; 54 if (Map [I] [J] ='s ') 55 {56 Si = I; 57 SJ = J; 58} else if (Map [I] [J] = 'D') 59 {60 di = I; 61 dj = J; 62} else if (Map [I] [J] = 'X') 63 {64 wall ++; 65} 66} 67} 68 if (N * m-wall <= T) // a small pruning 69 {70 cout <"no" <Endl; 71 continue; 72} 73 map [Si] [SJ] = 'X'; 74 DFS (Si, SJ, 0); 75 if (OK) 76 {77 cout <"yes" <Endl; 78} else79 {80 cout <"no" <Endl; 81} 82} 83 return 0; 84}

Use the code to talk about the DFS process:

Starting from S, I = 0, exploring to the right, as long as there is no return, it will always go to the right, return will be traced back, the process of backtracking, I = 0 to I = 1. This is the implementation process of backtracking...

 

TipsWhen initializing this maze matrix, I = 0, j = 0, I = n + 1, j = m + 1 are all initialized, but data is not stored, this is equivalent to adding a wall on all sides of the maze. In this way, you do not have to judge whether the system is out of bounds during the DFS process...

 

Next we will talk about pruning:

1. If the number of accessible blocks is smaller than T, it cannot be reached. This is the small pruning in main ().

2,Parity pruning:

You can think of map as follows: 0 1 0 1 0 11 0 1 0 1 00 1 0 1 0 0 11 0 1 00 1 0 1 0 1 1 take a step from the grid with 0, A grid with a value of 1 must take one step from a grid with a value of 1, and it must move toward a grid with a value of 0: 0-> 1 or 1-> 0 must be an odd number of steps. 0-> 0. 1-> 1 must be an even number. Conclusion:

Therefore, if the request time is from 0 to 0 but the request time is odd, or if the request time is from 1 to 0 but the request time is even, the system can directly determine whether the request is reachable!

This is the pruning in DFS, that is, the main pruning, where the & and operation is used to determine whether it is an even number...

 

Reminder:

Search is the most basic and commonly used algorithm. Here, some beginners do not pay much attention to pruning when learning these basic search algorithms, which is very undesirable, because all the questions you search for won't have a large scale for your test cases, And you often don't notice the running time, however, the real test data must be able to filter out algorithms without pruning.

In fact, contestants basically use common search algorithms. The differentiation of a question is usually based on optimization such as pruning. "

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.