Step-by-step write algorithm (A * algorithm)

Source: Internet
Author: User

Original: Step-by-step write algorithm (A * algorithm)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In the previous blog, we have already discussed the algorithm for finding the path. However, in the example diagram at that time, the optional path is unique. We choose an algorithm, that is, to choose the only path, how to choose? At that time we were using an exhaustive recursive algorithm. Today, however, the situation is a bit different. Where is it? That is, today's path has n, this path can reach the destination, however, we have a request in the process of selection, that is the shortest path selection? Is there any way to do it?

Then, a * algorithm can come in handy. What is the difference between a * algorithm and an ordinary algorithm? We can use an example to illustrate:

/**       0  0  0  0  0*       1  1  1  1  1*       1  0  0  0  1  *       1  0  0  0  1   *       A  1  1  1  1*/
This is an array of 5*5. Suppose we start with array[1][0] and the target is point a. We found that there are two ways to reach the destination in the diagram, but the shortest way down. So how to find this shortest algorithm? Friends can think for a moment.

Can we take time back to the first few steps we arrived at? Why do we have to choose the direction down point instead of the horizontal point? The reason is not complicated, because at all points, the distance between this point and the target point we want to select is the shortest. So, is there any change in the choice of paths in this middle? In fact, it is possible, because the process of selection of the province is a PK process, we can do is to find that the closest point to the target, and this point is the moment of change, so the last choice of the road should be such.

/**       0  0  0  0  0*       1  0  0  0  0*       1  0  0  0  0  *       1  0  0  0  0   *       A  0  0  0  0*/
Algorithm programming algorithm, how should modify it? First of all, define a data structure?

typedef struct _value{int x;int y;} VALUE;
And then, looking for the shortest distance to the target point,

int Find_most_nearest_neigh (VALUE data[], int length, int x, int y) {int index;int number;int current;int median;if (NULL = = Data | | 0 = = length) Return-1;current = 0;number = (int) sqrt ((data[0].x-x) * (data[0].x-x) + (data[0].y-y) *  (DATA[0].Y -y)); for (index = 1; index < length; index + +) {median = (int) sqrt ((data[index].x-x) * (data[index].x-x) + (Data[ind EX].Y-Y) *  (data[index].y-y)); if (median < number) {Number = Median;current = index;}} return current;}
Find this point, everything is good, then now we need to re-processing data, after all, some points need to pop up, there are some new points need to press into the processing.

value* Updata_data_for_queue (value* data, int length, int* newlen) {int index;int count;int max; value* pdata;if (NULL = = Data | | 0 = = Length | | NULL = = Newlen) return Null;max = length << 2;pdata = (value*) malloc (max * sizeof (VALUE)); memset (pData, 0, max * size Of (VALUE), Count = 0;for (index = 0; index < length; index + +) {if (Check_pos_valid (data[index].x, data[index].y-1)) {PDa ta[count].x = Data[index].x;pdata[count].y = Data[index].y-1;count + +;} if (Check_pos_valid (Data[index].x-1, data[index].y)) {pdata[count].x = Data[index].x-1;pdata[count].y = Data[index].y ; count + +; }if (Check_pos_valid (data[index].x, DATA[INDEX].Y + 1)) {pdata[count].x = Data[index].x;pdata[count].y = Data[index].y + 1;count + +;} if (Check_pos_valid (data[index].x + 1, data[index].y)) {pdata[count].x = data[index].x + 1;pdata[count].y = data[index].y ; count + +; }}*newlen = Count;return PData;}

With the above function, the Find_path is very simple.

void Find_path (int x, int y) {while  (/* Minimum distance not 0 */) {/  * update list */  * Find nearest Point */  };}


Summarize:

(1) A * focus on the design of weight judgment function, select the best next hop

(2) A * target is known

(3) A * is especially suitable for finding the path of a mesh type




Step-by-step write algorithm (A * algorithm)

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.