[Knowledge Point] A * search (heuristic search)

Source: Internet
Author: User

This blog post for the migration, written on April 4, 2015, does not represent my current views and views. Original address: http://blog.sina.com.cn/s/blog_6022c4720102vwud.html

1. PrefaceTree-General searches are DFS and BFS. A * algorithm, in fact, simple, is smart, say more complex, a * algorithm's future and role is very big-he even involves artificial intelligence (AI). So A * is a promising one.   2. ConceptA * is also searched with the BFS framework. is actually the enhanced version of the BFS. He needs to define a function, set to H (X), called the valuation function. The definition of a valuation function has many forms. Let me give you a very simple example://Standard diagram is missing
defines a Cartesian coordinate system, S is the starting point and T is the end point. The middle of the dark blue place for the non-movable heights. Let's start by analyzing the efficiency of using DFS or BFS: Starting with S, it needs to traverse the entire map, which is very slow. In this case, a * search is definitely faster, but a * must define the valuation function reasonably enough. The valuation function is described in detail below.   3. Valuation functionwhen searching, the definition of distance can generally be expressed as:  f=g+h (G is the depth at which the start of the search is at that point, and H is the valuation function) in, the method used is called Manhattan (Manhattan Distance), which is the number of moving steps that reach the target point from the point of consideration by horizontal and vertical movement. Note just move horizontally and vertically, without taking a slash. and ignores obstacles in the diagram. -----------------------------------------------------------------------------------------------------int getval (int x,int y,int dep) {return (ABS (X-TX) +abs (y-ty) +DEP);}//ABS is absolute-----------------------------------------------------------------------------------------------------Note that not all A * Search's valuation function is the same, such as "Eight digital puzzles", "Target Sudoku" required for the valuation function is not the same! Moreover, here only the most common valuation function, the specific problem to be specific analysis, you need to try the most appropriate evaluation function step-by-step.   4. SearchAccording to the above valuation function, the current state can be obtained://Standard diagram is missing

when initializing, we save the node s in an array (it is not possible to use a normal array, and we will say how to do it later). Node S starts searching around 8 nodes to get the F value. This round of search is over. In the next round, all nodes that have not yet been traversed (that is, the previously saved node) are identified with the smallest F value, minnode, and Minnode removed from the array.

continue searching from Minnode to get more nodes to save. Search repeatedly until there are no nodes in the array ( note!) The end of the search can not be directly exit! Think about it, why ), the search is over.    5. How to store and traverseas mentioned above, arrays are obviously not possible. We need to open a struct to hold the x,y,val,dep of each node. Val record f value. As you traverse, you can see that the queue needs to be queued for use. However, every time we need to find the node is the lowest f value Ah! Rather than FIFO. So, the "heap" (http://blog.sina.com.cn/s/blog_6022c4720102vss0.html) mentioned earlier has a role to play, not just heap sorting. Heap, where it is actually more appropriate to use another name--priority queue. each time we select the lowest f value from the queue, it is easy to implement. in order to "unconventional", under the cab led, I used the system priority Queue (This is my first time with the STL data structure ....) )-----------------------------------------------------------------------------------------------------priority_queue<node,vector<node>,cmp> Q;-----------------------------------------------------------------------------------------------------This is how the priority queue is defined and requires a queue library, a vector library. Here is the structure of the priority queue, between the "vector<node>" is the default format, specific reasons I do not know: Anyway, it's OK if you need to define an integer priority queue and change node to int. CMP is the preferred queue comparison method, which you write yourself, similar to sort (str+1,str+n+1,cmp). Use the same way as the normal queue, the specific self-check it ... I think it's very understandable. STL is a variety of limitations.   6. CodeThe simplest Cartesian coordinate system runs the shortest way. Enter the 0-1 map and the start point. Find out how many steps you need to take. -----------------------------------------------------------------------------------------------------#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <cstdlib>#define INF 1<<30#define MAXN 1005using namespace std; struct Node{int x,y,val,dep,dist;node () {}node (int a, int b) {x=a; y=b;}};node PATH[MAXN]; struct CMP//comparison method{bool Operator () (node A,node b){return (a.val>b.val);}}; int SX,SY,TX,TY,NX,NY,MAP[MAXN][MAXN];priority_queue<node,vector<node>,cmp> Q; int n,s,t,x[maxn],y[maxn],now; int abs (int a) {return (a<0)-a:a;} int getval (int x,int y,int dep) {return (ABS (X-TX) +abs (y-ty) +DEP);} int Getman (int x,int y) {return (ABS (X-SX) +abs (Y-sy));} int check (int x,int y){return (x>0 && y>0 && x<=map[nx][ny]);} void BFS (){node S=node (sx,sy); s.dep=0; s.dist=0;Q.push (s);While (Q.empty ()!=1){node Minnode=q.top ();nx=minnode.x; ny=minnode.y;if (minnode.x==tx && minnode.y==ty){printf ("%d", minnode.dist);exit (0);}Q.pop ();if (check (nx,ny+1) ==1){node Temp=node (nx,ny+1);Temp.dep=getman (nx,ny+1);Temp.val=getval (NX,NY+1,TEMP.DEP);temp.dist=minnode.dist+1;MAP[NX][NY+1]=TEMP.DEP;Q.push (temp);}if (check (nx,ny-1) ==1){node Temp=node (nx,ny-1);Temp.dep=getman (nx,ny-1);Temp.val=getval (NX,NY-1,TEMP.DEP);temp.dist=minnode.dist+1;MAP[NX][NY-1]=TEMP.DEP;Q.push (temp);}if (check (nx+1,ny) ==1){node Temp=node (nx+1,ny);Temp.dep=getman (nx+1,ny);Temp.val=getval (NX+1,NY,TEMP.DEP);temp.dist=minnode.dist+1;MAP[NX+1][NY]=TEMP.DEP;Q.push (temp);}if (check (nx-1,ny) ==1){node Temp=node (nx-1,ny);Temp.dep=getman (nx-1,ny);Temp.val=getval (NX-1,NY,TEMP.DEP);temp.dist=minnode.dist+1;MAP[NX-1][NY]=TEMP.DEP;Q.push (temp);}}} void init (){freopen ("astar.in", "R", stdin);freopen ("Astar.out", "w", stdout);scanf ("%d", &n);for (int i=1;i<=n;i++)for (int j=1;j<=n;j++) {scanf ("%d", &map[i][j]), if (map[i][j]==1) Map[i][j]=inf;}scanf ("%d%d%d%d", &sx,&sy,&tx,&ty);} int main (){init ();BFS ();return 0;}-----------------------------------------------------------------------------------------------------

[Knowledge Point] A * search (heuristic search)

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.