A * (also called a Star, a star) pathfinding algorithm Java edition

Source: Internet
Author: User

There are many ways to find a path, and a * pathfinding algorithm is recognized as the best path finding algorithm.

First of all, to understand what is a * pathfinding algorithm, you can refer to these three articles:

HTTP://WWW.GAMEDEV.NET/PAGE/RESOURCES/_/TECHNICAL/ARTIFICIAL-INTELLIGENCE/A-PATHFINDING-FOR-BEGINNERS-R2003 (English)

Http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html (English)

Http://www.cnblogs.com/technology/archive/2011/05/26/2058842.html (English)


original articles, reproduced please specify the source:http://blog.csdn.net/ruils/article/details/40780657


The following is a test map, 0 means passable, and 1 means obstacles:


To go from point (5, 1) to points (5, 5), find the path as @ by the * pathfinding algorithm:


In the code, you can modify the obstacles, the start and end points to test the algorithm.

Final code:


Import Java.util.arraylist;import Java.util.list;public class AStar {public static final int[][] NODES = {{0  , 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0,  1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0,    0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},};    public static final int STEP = 10;    Private arraylist<node> openlist = new arraylist<node> ();    Private arraylist<node> closelist = new arraylist<node> ();        Public Node findminfnodeinopnelist () {Node Tempnode = openlist.get (0); for (node node:openlist) {if (node.            F < TEMPNODE.F) {tempnode = node;    }} return Tempnode; } public arraylist<node> Findneighbornodes (Node currentnode) {arraylist<node> ArrayList = new Arraylist<node> ();        Only consider the upper and lower left and right, regardless of oblique diagonal int topX = currentnode.x;        int topy = currentnode.y-1;        if (Canreach (TopX, topy) &&!exists (closelist, TopX, topy)) {Arraylist.add (new Node (TopX, topy));        } int bottomx = currentnode.x;        int bottomy = currentnode.y + 1; if (Canreach (BOTTOMX, bottomy) &&!exists (closelist, BOTTOMX, bottomy)) {Arraylist.add (new Node (bottom        X, bottomy));        } int leftx = currentnode.x-1;        int lefty = CURRENTNODE.Y; if (Canreach (LEFTX, lefty) &&!exists (closelist, LEFTX, lefty)) {Arraylist.add (new Node (LEFTX, lefty))        ;        } int rightx = currentnode.x + 1;        int righty = CURRENTNODE.Y; if (Canreach (RIGHTX, righty) &&!exists (closelist, RIGHTX, righty)) {Arraylist.add (new Node (RIGHTX, RI        Ghty));    } return arrayList;       } public boolean Canreach (int x, int y) { if (x >= 0 && x < nodes.length && y >= 0 && y < nodes[0].length) {return        Nodes[x][y] = = 0;    } return false;        } public node Findpath (node Startnode, node Endnode) {//Adds the starting point to the Open list Openlist.add (startnode); while (Openlist.size () > 0) {//Walk through the open list, look for the node with the lowest F value as node CurrentNode = FINDM            Infnodeinopnelist ();            Remove Openlist.remove (CurrentNode) from the open list;            Move this node to the close list Closelist.add (CurrentNode);            arraylist<node> neighbornodes = Findneighbornodes (CurrentNode); for (node Node:neighbornodes) {if (Exists (openlist, Node)) {Foundpoint (CurrentNode,                node);                } else {notfoundpoint (CurrentNode, Endnode, node); }} if (Find (Openlist, endnode)! = null) {return find (openlist, Endnode);    }} return find (Openlist, endnode);        } private void Foundpoint (node Tempstart, node node) {int G = CALCG (tempstart, node); if (G < node.            G) {node.parent = Tempstart; Node.            g = g;        NODE.CALCF ();        }} private void Notfoundpoint (node Tempstart, node end, node node) {node.parent = Tempstart; Node.        G = CALCG (tempstart, node); Node.        H = Calch (end, node);        NODE.CALCF ();    Openlist.add (node);        } private int CALCG (node Start, node node) {int G = STEP; int PARENTG = node.parent! = null?        node.parent.g:0;    return G + PARENTG;        } private int Calch (node end, node node) {int step = Math.Abs (node.x-end.x) + math.abs (NODE.Y-END.Y);    return step * STEP;        } public static void Main (string[] args) {node startnode = new Node (5, 1);        Node Endnode = new Node (5, 5); Node parent = new AStar (). Findpath (STartnode, Endnode); for (int i = 0, i < nodes.length; i++) {for (int j = 0; J < Nodes[0].length; J + +) {Syste            M.out.print (Nodes[i][j] + ",");        } System.out.println ();        } arraylist<node> ArrayList = new arraylist<node> ();            while (parent! = NULL) {//System.out.println (Parent.x + "," + parent.y);            Arraylist.add (New Node (Parent.x, Parent.y));        parent = parent.parent;        } System.out.println ("\ n"); for (int i = 0, i < nodes.length; i++) {for (int j = 0; J < Nodes[0].length; J + +) {if (E                Xists (ArrayList, I, J)) {System.out.print ("@,");                } else {System.out.print (Nodes[i][j] + ",");        }} System.out.println (); }} public static node find (list<node> nodes, node point) {for (node N:nodes) if ((n.x = = point.x) && (N.Y = = Point.y)) {return n;    } return null; The public static Boolean exists (List<node> nodes, node-node) {for (node N:nodes) {if (n.x =            = node.x) && (N.Y = = node.y)) {return true;    }} return false; The public static Boolean exists (List<node> nodes, int. x, int y) {for (Node n:nodes) {if (n.            x = = x) && (N.Y = = y)) {return true;    }} return false;            public static class Node {public node (int x, int y) {this.x = x;        This.y = y;        } public int x;        public int y;        public int F;        public int G;        public int H; public void Calcf () {this. F = this. G + this.        H    } public Node parent; }}




A * (also called a Star, a star) pathfinding algorithm Java edition

Related Article

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.