Text: One-step-one-step write algorithm (pathfinding)
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Pathfinding is a function that needs to be used in game design, so how do we find the target point quickly with a point as the starting point? In fact, the way to find the road is not difficult. A simple and effective method is the backtracking method. If we start from a point, then there must be a number of roads around this point, as long as there is a road to exist, we continue to go, until we find no way to go, if we find that the road can not go down how to do, then we have to turn back, we have to choose from the remaining options, continue to try. If unfortunately, all the attempts are over, or the target node is not found, it can only be explained that we really have no way to go.
A) First, we use matrices to represent the map: where 1 represents the road, 0 means no road, 2 means the end point, and the starting point is (1,0)
#define MAX_NUMBER_LENGTH 6static 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 road that has passed */
b) In fact, we write a judgment function that determines whether the current node is legitimate
int check_pos_valid (int x, int y) {/* node is out of bounds */if (x < 0 | | x>= max_number_length | | y < 0 | | y >= max_number_l Ength) return 0;/* whether the current node exists */if (0 = = Gpath[x][y]) return 0;/* whether the current node has traversed */if (' # ' = = Gvalue[x][y]) return 0;return 1;
c) Next, we write a recursive search algorithm to
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) In order to verify that our algorithm is correct, we can write 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");}}
e) The algorithm described in C above is only looking for a path, so if you want to traverse all the roads, how should the algorithm be modified?
void Find_path (int x, int y) {if (Check_pos_valid (x, y)) {if (2 = = Gpath[x][y]) {gvalue[x][y] = ' # ';p rint_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;}}
Study Questions:
The above topic introduces the way to find the path, and describes how to traverse all possible paths. Of course you can find a shortest path from all of these search paths. But friends can think about it, is there a way to find the best path in a moment?
Step-by-step write algorithm (pathfinding)