Package Com.test;import Java.util.arraylist;import Java.util.collections;import java.util.comparator;import Java.util.list;public class AStar {private int[][] map;//map (1 via 0 not available) Private list<node> openlist;//Open list Private list<node> closelist;//Close list private final int cost_straight = 10;//A path that moves vertically or horizontally the private final int cost_diagonal = 14;//Direction-shifted path scoring private int row;//line private int column;//column public AStar (int[][] map, int row, int column) {this.map = m Ap;this.row = Row;this.column = Column;openlist = new arraylist<node> (); closelist = new arraylist<node> ();} Find Coordinates (-1: Error, 0: not found, 1: found) public int search (int x1, int y1, int x2, int y2) {if (x1 < 0 | | x1 >= Row | | x2 < 0 | | X2 >= Row | | Y1 < 0| | Y1 >= Column | | Y2 < 0 | | Y2 >= column) {return-1;} if (map[x1][y1] = = 0 | | map[x2][y2] = = 0) {return-1;} Node SNode = new node (x1, y1, null); Node Enode = new node (x2, y2, null); Openlist.add (SNode); list<node> resultlist = Search (SNode, enode);if (resultlist.size () = = 0) {return 0;} for (Node node:resultlist) {map[node.getx ()][node.gety ()] =-1;} return 1;} Find core algorithm private list<node> Search (node SNode, node Enode) {list<node> resultlist = new arraylist<node> (); Boolean isfind = false; Node node = null;while (openlist.size () > 0) {//takes out the lowest f value in the open list, that is, the first stored value f is the lowest node = openlist.get (0);//Determines if the target point if (node) is found. GetX () = = Enode.getx () && node.gety () = = Enode.gety ()) {isfind = True;break;} Upper if ((Node.gety ()-1) >= 0) {Checkpath (Node.getx (), Node.gety ()-1, node, enode,cost_straight);} Under If ((Node.gety () + 1) < column) {Checkpath (Node.getx (), node.gety () + 1, node, enode,cost_straight);} Left if ((Node.getx ()-1) >= 0) {Checkpath (Node.getx ()-1, node.gety (), node, enode,cost_straight);} Right if ((Node.getx () + 1) < row) {Checkpath (Node.getx () + 1, node.gety (), node, enode,cost_straight);} Upper left if ((Node.getx ()-1) >= 0 && (node.gety ()-1) >= 0) {Checkpath (Node.getx ()-1, Node.gety()-1, node, enode,cost_diagonal);} Lower left if ((Node.getx ()-1) >= 0 && (node.gety () + 1) < column) {Checkpath (Node.getx ()-1, node.gety () + 1, node, enode,cost_diagonal);} Upper right if ((Node.getx () + 1) < row && (Node.gety ()-1) >= 0) {Checkpath (Node.getx () + 1, node.gety ()-1, nod E, enode,cost_diagonal);} Lower right if ((Node.getx () + 1) < row && (Node.gety () + 1) < column) {Checkpath (Node.getx () + 1, node.gety () + 1, node, enode,cost_diagonal);} Remove//Add from the open list to the Closelist.add (openlist.remove (0));//Open the list and place the lowest F value at the bottom collections.sort (openlist, new Nodefcomparator ());} if (isfind) {GetPath (resultlist, node);} return resultlist;} Query whether this path can walk through private boolean checkpath (int x, int y, node parentnode, node Enode,int cost) {node node = new node (x, Y, pare Ntnode);//Find out if the map can be passed if (map[x][y] = = 0) {closelist.add (node); return false;} Find out if there is an if (Islistcontains (closelist, x, y) =-1) {return false;} in the Close list Find out if an int index = -1;if (index = Islistcontains (o) is present in the listPenlist, x, y)) =-1) {//G value is smaller, that is, whether to update the G,F value if ((PARENTNODE.GETG () + cost) < Openlist.get (index). GETG ()) {node.setparent Node (parentnode); COUNTG (node, enode, cost); countf (node); Openlist.set (Index, node);}} else {//Added to the Open list Node.setparentnode (parentnode); count (node, enode, cost); Openlist.add (node);} return true;} Whether the collection contains an element (-1: Not found, otherwise returned by index) private int islistcontains (list<node> List, int x, int y) {for (int i = 0; i < l Ist.size (); i++) {Node node = list.get (i); if (node.getx () = = x && node.gety () = = y) {return i;}} return-1;} Return from the endpoint to the beginning private void GetPath (list<node> resultlist, node node) {if (Node.getparentnode ()! = null) {GetPath (resu Ltlist, Node.getparentnode ());} Resultlist.add (node);} Calculates the g,h,f value private void count (node node, node enode, int cost) {COUNTG (node, enode, cost); Counth (node, enode); countf (Enode );} Calculates the G-value private void COUNTG (node node, node enode, int cost) {if (node.getparentnode () = = null) {NODE.SETG);} else {No DE.SETG (Node.getparentnode ().GETG () + cost);}} Computes the H value private void Counth (node node, node Enode) {int x = Math.Abs (Node.getx ()-Enode.getx ()), int y = Math.Abs (Node.gety ( )-enode.gety ()); if (x<y) {node.seth (x * cost_diagonal + (y-x) * cost_straight);} Else{node.seth (Y * cost_diagonal + (x-y) * cost_straight);}} Calculates the F-value private void countf (node node) {NODE.SETF (NODE.GETG () + Node.geth ());} public static void Main (string[] args) {int[][] map=new int[][]{//map Array {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,0,1,1,1,1,1}, {1,1,1,1,0,1,1,1,1,1}, {1,1,1,1,0,1,1,1,1,1}, {1, 1, 1,1,0,1,1,1,1,1}, {1,1,1,1,0,1,1,1,1,1}}; AStar astar=new AStar (map, 6, 10); int Flag=astar.search (4, 0, 3, 8); if (flag==-1) {System.out.println ("Transfer data wrong! "); }else if (flag==0) {System.out.println ("not Found! "); }else{for (int x=0;x<6;x++) {for (int y=0;y<10;y++) {if (map[x][Y]==1) {System.out.print (""); }else if (map[x][y]==0) {System.out.print ("the"); }else if (map[x][y]==-1) {System.out.print ("※"); }} System.out.println (); }}}}//node classes class Node {private int x;//x coordinate private int y;//y coordinate private node parentnode;//parent class node private int g;// The current point-to-start movement consumes private int h;//The current point-to-end movement cost, which is the Manhattan distance |x1-x2|+|y1-y2| (ignoring obstructions) private int f;//f=g+hpublic Node (int x, int y, Node parentnode) {this.x = X;this.y = Y;this.parentnode = Parentnod e;} public int GetX () {return x;} public void SetX (int x) {this.x = x;} public int GetY () {return y;} public void sety (int y) {this.y = y;} Public Node Getparentnode () {return parentnode;} public void Setparentnode (Node parentnode) {this.parentnode = parentnode;} public int Getg () {return g;} public void Setg (int g) {this.g = g;} public int Geth () {return h;} public void SetH (int h){this.h = h;} public int getf () {return F;} public void setf (int f) {this.f = f;}} Node comparison classes class Nodefcomparator implements comparator<node> {@Overridepublic int compare (node O1, node O2) {return O1. GETF ()-O2.GETF ();}}
Reference: Http://www.raywenderlich.com/4946/introduction-to-a-pathfinding
A-Star Algorithm--java revision