[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Pathfinding is a function that needs to be used in game design. How can we quickly find the target point using a point as the starting point? In fact, it is not difficult to find the path. A simple and effective method is the Backtracking Method. If we start from a point, there will certainly be several roads around this point. As long as there is one path, we will keep going until we find there is no way to go; if we find that the path cannot go, we have to go back. We can only choose one path from the remaining options and continue to try again. If, unfortunately, all the attempts are over and the target node is still not found, it only means that we have no way to go.
A) First, we use a matrix to represent a map. Where 1 represents a path, 0 represents no path, 2 represents an end point, and the start point is)
# Define MAX_NUMBER_LENGTH 6
Static int gPath [MAX_NUMBER_LENGTH] [MAX_NUMBER_LENGTH] = {
{0, 0, 0, 0, 1, 1 },
{1, 1, 0, 0, 1, 0 },
{0, 1, 1, 1, 1, 0 },
{0, 0, 1, 0, 1, 2 },
{0, 0, 1, 0, 1, 0 },
{0, 0, 1, 1, 1, 0}
};
Static int gValue [MAX_NUMBER_LENGTH] [MAX_NUMBER_LENGTH] = {0};/* record the path that has elapsed */
# Define MAX_NUMBER_LENGTH 6
Static int gPath [MAX_NUMBER_LENGTH] [MAX_NUMBER_LENGTH] = {
{0, 0, 0, 0, 1, 1 },
{1, 1, 0, 0, 1, 0 },
{0, 1, 1, 1, 1, 0 },
{0, 0, 1, 0, 1, 2 },
{0, 0, 1, 0, 1, 0 },
{0, 0, 1, 1, 1, 0}
};
Static int gValue [MAX_NUMBER_LENGTH] [MAX_NUMBER_LENGTH] = {0};/* record the path that has elapsed */
B) In fact, We compile a judgment function to determine whether the current node is valid.
Int check_pos_valid (int x, int y)
{
/* Determine whether a node has a boundary */
If (x <0 | x> = MAX_NUMBER_LENGTH | y <0 | y> = MAX_NUMBER_LENGTH)
Return 0;
/* Whether the current node has a path */
If (0 = gPath [x] [y])
Return 0;
/* Whether the current node has passed */
If ('#' = gValue [x] [y])
Return 0;
Return 1;
}
Int check_pos_valid (int x, int y)
{
/* Determine whether a node has a boundary */
If (x <0 | x> = MAX_NUMBER_LENGTH | y <0 | y> = MAX_NUMBER_LENGTH)
Return 0;
/* Whether the current node has a path */
If (0 = gPath [x] [y])
Return 0;
/* Whether the current node has passed */
If ('#' = gValue [x] [y])
Return 0;
Return 1;
}
C) then, compile a recursive search algorithm.
Int find_path (int x, int y)
{
If (check_pos_valid (x, y ))
{
If (2 = gPath [x] [y]) {
GValue [x] [y] = '#';
Return 1;
}
GValue [x] [y] = '#';
If (find_path (x, Y-1 ))
Return 1;
If (find_path (x-1, y ))
Return 1;
If (find_path (x, y + 1 ))
Return 1;
If (find_path (x + 1, y ))
Return 1;
GValue [x] [y] = 0;
Return 0;
}
Return 0;
}
Int find_path (int x, int y)
{
If (check_pos_valid (x, y ))
{
If (2 = gPath [x] [y]) {
GValue [x] [y] = '#';
Return 1;
}
GValue [x] [y] = '#';
If (find_path (x, Y-1 ))
Return 1;
If (find_path (x-1, y ))
Return 1;
If (find_path (x, y + 1 ))
Return 1;
If (find_path (x + 1, y ))
Return 1;
GValue [x] [y] = 0;
Return 0;
}
Return 0;
}
D) To verify whether our algorithm is correct, you can compile a print function.
Void print_path ()
{
Int outer;
Int inner;
For (outer = 0; outer <MAX_NUMBER_LENGTH; outer ++ ){
For (inner = 0; inner <MAX_NUMBER_LENGTH; inner ++ ){
Printf ("% c", gValue [outer] [inner]);
}
Printf ("\ n ");
}
}
Void print_path ()
{
Int outer;
Int inner;
For (outer = 0; outer <MAX_NUMBER_LENGTH; outer ++ ){
For (inner = 0; inner <MAX_NUMBER_LENGTH; inner ++ ){
Printf ("% c", gValue [outer] [inner]);
}
Printf ("\ n ");
}
}
E) The algorithm described in c is just a path. How can I modify the algorithm if I want to traverse all roads?
Void find_path (int x, int y)
{
If (check_pos_valid (x, y ))
{
If (2 = gPath [x] [y]) {
GValue [x] [y] = '#';
Print_path ();
GValue [x] [y] = 0;
Return;
}
GValue [x] [y] = '#';
Find_path (x, Y-1 );
Find_path (x-1, y );
Find_path (x, y + 1 );
Find_path (x + 1, y );
GValue [x] [y] = 0;
}
}
Void find_path (int x, int y)
{
If (check_pos_valid (x, y ))
{
If (2 = gPath [x] [y]) {
GValue [x] [y] = '#';
Print_path ();
GValue [x] [y] = 0;
Return;
}
GValue [x] [y] = '#';
Find_path (x, Y-1 );
Find_path (x-1, y );
Find_path (x, y + 1 );
Find_path (x + 1, y );
GValue [x] [y] = 0;
}
}
Questions:
This topic describes how to find a path and how to traverse all possible paths. Of course, you can find the shortest path from all the search paths. But my friends can think about it. Is there a way to find the optimal path at once?