The principle and implementation of a * algorithm

Source: Internet
Author: User

One: introduction of A *

A * algorithm is a heuristic search algorithm that evaluates the location of each search in the state space, obtains the best position, and then searches from this location to the target. This can omit a large number of intrepid search paths and mention efficiency. In heuristic search, it is very important to estimate the location. Different valuations can be used to have different effects.

The algorithm can be expressed in equation F (n) =g (n) +h (n),whichf (N)is from the initial point through the nodeNvaluation function to the target point, g (N)is in the state spaceFrom the initial node to theNthe actual cost of the node,h (N)is fromNThe estimated cost of the best path to the target node. h* (n) is the actual cost of the best path from n to the target node, then the entire heuristic search process must be guaranteed to h (n) <=h* (n), otherwise the search will fail.

For the choice of H (N), the closer the h* (n) is, the faster it is.

There are several heuristic functions in the main

Manhattan Distance:

The formal meaning of the Manhattan distance is defined as the distance between the l1-distance or the city block, which is the sum of the distance between the two points in the fixed Cartesian coordinates of the Euclidean space and the projection generated by the axis. For example, in the plane, the coordinates (X1,Y1) point P1 with coordinates (x2, y2) points P2 the Manhattan distance: |x1-x2| + |y1-y2|.

Euclidean distance:

is a commonly used distance definition, which is the true distance between two points in an m-dimensional space. The Euclidean distance in two and three-dimensional space is the distance between two points. For example, on a plane, the point P1 of the coordinates (X1,Y1) and the point P2 of coordinates (x2, y2) are Euclidean distances: sqrt ((x1-x2) ^2+ (y1-y2) ^2).

Chebyshev Snow Distance:

is the maximum value of each component difference between two vectors. For example, in the plane, the coordinates (x1, y1) point P1 with the coordinates (x2, y2) of the point P2 the Chebyshev distance: Max (|x1-x2|, |y1-y2|).

Two: The process of a * algorithm

Goal: To go from A to P, the value behind the node is



Two tables are set during the search: open and closed. The Open table holds all the nodes that have been generated without being inspected, and the nodes that have been visited are recorded in the closed table. One step in the algorithm is to rearrange the open table based on the valuation function. Each step in this loop considers only the best nodes in the Open table. The specific search process is as follows:

1) Initial state:
Open=[a5];closed=[];
2) estimate the A5, obtain the child nodes, and put into the open table;
OPEN=[B4,C4,D6];CLOSED=[A5]
3) estimate the B4, obtain the child nodes, and put into the open table;
OPEN=[C4,E5,F5,D6];CLOSED=[B4,A5]
4) Estimating C4; Obtaining a child node and putting it into the open table;
OPEN=[H3,G4,E5,F5,D6];CLOSED=[C4,B4,A5]
5) estimate the H3, obtain the child nodes, and put into the open table;
OPEN=[O2,P3,G4,E5,F5,D6];CLOSED=[H3,C4,B4,A5]
6) estimate the O2, obtain the child nodes, and put into the open table;
OPEN=[P3,G4,E5,F5,D6];CLOSED=[O2,H3,C4,B4,A5]
7) Estimated P3, has been solved;


Where the open table can be implemented with a priority queue

Code:

POJ2243

On the chessboard, the knight walks the "Day" form, seeking the shortest steps from the chessboard to the other point?

#include <stdio.h> #include <queue> #include <string.h> #include <math.h> using namespace std;  int a[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}},vist[9][9];      struct node {int x,y,step;      int f,g,h;      BOOL operator < (const node &another) const {return f > another.f;  }  };    Node s,e;  int Find_h (node tmp) {double a;a= ((e.x-tmp.x) ^2+ (E.Y-TMP.Y) ^2); int b=sqrt (a); return b*10;}    priority_queue<node>que;      int AStar (node Start,node end) {int i;      Node Cur,next;      while (!que.empty ()) Que.pop ();      Que.push (start);          while (!que.empty ()) {cur = que.top ();          Que.pop ();              for (i=0;i<8;i++) {next.x = cur.x + a[i][0];              Next.y = Cur.y + a[i][1]; if (!vist[next.x][next.y] && next.x>=1 && next.x<=8 && next.y>=1 && next.y< =8) {vist[NEXt.x] [Next.y] = 1;                  Next.step = Cur.step + 1;                  NEXT.G = Cur.g +23;                  Next.h = Find_h (next);                    NEXT.F = next.g + next.h;                  if (next.x = = End.X && Next.y = = end.y) return next.step;              Que.push (next);  }}} return 0;      } int main () {int move;      Char t,m;          while (scanf ("%c%d%c%d", &t,&s.y,&m,&e.y)!=eof) {GetChar ();          memset (vist,0,sizeof (vist));          move = 0;          s.x = t-' a ' + 1;          e.x = M-' a ' + 1;          S.step = 0;          S.G = 0;          S.F = S.G + find_h (s);          Vist[s.x][s.y]=1;          move = AStar (s,e);      printf ("To get from%c%d to%c%d takes%d Knight moves.\n", T,s.y,m,e.y,move);  } return 0;   }


The principle and implementation of 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.