Getting Started with Search

Search ------ find the target state in the solution space

An algorithm that searches for a solution through constant trial and error. It's more of a method than an algorithm. The basic methods include violent search, deep search, and wide search. More advanced ones are IDDFS, DBFS, A*, IDA* and so on. A method that uses the high performance of the computer to purposefully enumerate part or all of the possible situations of a problem solution space, so as to find the solution to the problem.

1. Depth-first search (DFS)

"One way to the dark.": DFS (Depth-First Search) is one of the means of search. It starts from a certain state, continuously transitions states until it cannot be transitioned, then returns to the state of the previous step, continues to transition to other states, and so on, until it finds the final solution. Example: Sudoku, full permutation (next_permutation), N queens problem

Algorithm process:

VOID DFS (state A)

1. Determine whether the current state is legal. If it is legal, it will continue to execute, otherwise it will return to the last call.

2. Go down one layer first, that is, call DFS (state A)

Example 1: Full permutation

void dfs (the k numbers have been fixed and the remaining numbers are all arranged, which k numbers are displayed by marking the number to be used or not)

{

  If k>n output has been obtained, return

  else i loop from 1 to n

  If i is not used, fix i at the current position and call dfs(k+1)

  After calling dfs(k+1), the i fixed at the current position needs to be taken away

}

  2: Queen N

  Since the placement of the queens cannot be determined by a certain formula, the placement of each queen must be tested and corrected , which is the idea of ​​"backtracking".

  Before N queens are placed, the heuristic method for placing the i-th queen and the i+1-th queen is the same, so it can be handled by a recursive method.

  Due to the special nature of the queen itself, that is, there can only be one queen in a row and a column, so all we have to do is to place it from row 0 to row n-1.

  (UVA167)

 

 3: Horse stepping on the chessboard

  Abstract problems, there is a person (that is, the horse in that board), you have to conduct a traversal of the entire map, not heavy;

  Next, consider how the horse moves (8 kinds);

  Then how to show this chessboard in the program;

  The last is to apply the main structure of the backtracking method;

The general framework of DFS:

  Test whether node A A is satisfied in this graph (or tree)

  If yes, the various values ​​affected by marker A if it has been probed;

  Next, to test all the nodes that A can reach;

  After all, you will be executed, restore the marker A;

 

2. Breadth-first search (BFS)

 

 

Guess you like

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