Backtracking method - the path in the interview question matrix (1)

Introduction to Backtracking

1.1
The backtracking method is an upgraded version of the brute force method, which systematically selects a feasible solution from all possible options in each step of solving the problem.
1.2
Backtracking works well for problems that consist of multiple steps, and each step has multiple options. When we make a choice at a certain step, we will go to the next step. If it does not meet the conditions of the topic, we will go back to the original step and make a new choice. conditional options. If none of them match, it will go back to the previous step, then enter and make a new selection, and repeat the selection in this way until it reaches the final state.
1.3
Usually the backtracking algorithm is suitable for implementing code recursively . When we reach a certain node, try all possible options and recursively reach the next node if the conditions are met.

Backtracking application (instantiation)

Backtracking Application Test Questions

Title:
Please design a function to determine whether there is a path containing all the characters of a string in a matrix. The path can start from any grid in the matrix, and each step can move one grid to the left, right, up, and down in the matrix. If a path passes through a cell of the matrix, the path cannot enter that cell again.
For example, the following 3*4 matrix contains a path of the string ""bfce" (the letters in the path are underlined). But the path of the string "abfb" is not included in the matrix, because the string After the first character b in the matrix occupies the second grid in the matrix, the path cannot enter this grid again.
insert image description here
Analysis of the backtracking method to solve the problem process
The second letter 'b' of the first row and the first character of the path 'bfce' Equal, so starting from b, there are 3 options for b, to the left to reach the character a, to the right to reach the character t, and to go down to the character f. If you go to the left for the first time, you will reach a, but
a is not in the path The second character, so it will go back to b, re-select again, until it reaches f once, it will continue to go down.
There are also 3 options for node f. If we choose to reach the character c to the left, but then from After leaving the character c on the left, it is found that no matter how you go, it is impossible to reach the character c, so you can only backtrack to reach the character f and then choose again. And so on until the string bfce is formed. Idea: first, choose in
the
matrix A grid as the starting point of the path. Assume that the character of a certain grid in the matrix is ​​ch, and this grid will correspond to the i-th character on the path.
If the i-th character on the path is not ch, then this grid cannot be in The i-th position on the path.
If the i-th character on the path happens to be ch, then go to the adjacent grid to find the i+1-th character on the path.
Except for the grid on the border of the matrix, other grids have 4 adjacent grids. Repeat this step until all the characters on the path find their corresponding positions in the matrix.
Due to the recursive nature of the backtracking method, the path can be regarded as a stack. After the position of the first n characters in the path is located in the matrix, the n+1th character is not found around the grid corresponding to the nth character. , at this time, we have to go back to the n-1th character on the path and reposition the nth character.
Since the path cannot repeatedly enter the grid of the matrix, it is also necessary to define a Boolean matrix with the same size as the character matrix to identify whether the path has entered each grid.

When the grid whose coordinates are (row, col) in the matrix is ​​the same as the character whose subscript is pathLength in the path string, from the four adjacent grids (row, col-1), (row-l, col) (row col+1) and (row+1, col) to locate the character whose subscript is pathLengh+1 in the path string.
If none of the 4 adjacent grids matches the character subscript pathLengh+1 in the string, it indicates that the character subscript pathLengh in the current path string is not positioned correctly in the matrix, and we need to go back to the previous character (pathLengh-1), and then relocate.
Repeat this process until all the characters on the path string find a suitable position in the matrix (at this time str[pathLength]=='\0').

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/129998875