一个A*算法的demo

自己研究A*算法的半成品,放这容易找到。基于JAVA控制台

 

package aStarFind;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;


public class AStar {
	
	private final int verticalBound = 10;
	private final int horizontalBound = 6;
	private int [][] map = {
			{0,0,1,0,0,0},
			{0,0,1,1,0,0},
			{0,0,1,1,0,0},
			{0,0,1,1,0,0},
			{0,0,1,1,0,0}
	};
	
	int[][] adjs = {{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
	
	Node start = new Node(2,0);
	Node exit = new Node(2,5);
	Node cur;
	Node next;
	
	PriorityQueue<Node> open = new PriorityQueue<AStar.Node>();
	List<Node> close = new ArrayList<AStar.Node>();
	
	class Node implements Comparable<Node>
	{
		int x,y;
		Node parent;
		int F,G,H;

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + x;
			result = prime * result + y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Node other = (Node) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (x != other.x)
				return false;
			if (y != other.y)
				return false;
			return true;
		}

		private AStar getOuterType() {
			return AStar.this;
		}

		public Node(int x, int y) {
			super();
			this.x = x;
			this.y = y;
			parent = null;
		}

		public Node() {
			super();
			parent = null;
		}

		public int compareTo(Node o) {
			if(F < o.F)
				return -1;
			if(F > o.F)
				return 1;
			return 0;
		}

		@Override
		public String toString() {
			return "Node [x=" + x + ", y=" + y + ", parent=" + parent + ", F="
					+ F + ", G=" + G + ", H=" + H + "]";
		}
		
	}
	
	/**
	 * 找到邻接节点
	 * @param n
	 * @return
	 */
	Node[] FindAdjs(Node n)
	{
		List<Node> list = new ArrayList<AStar.Node>();
		for(int i = 0,length = adjs.length; i < length; i++)
		{
			int ax = adjs[i][0];
			int ay = adjs[i][1];
			int x = n.x + ax;
			int y = n.y + ay;
			if(x >= 0 && x < verticalBound && y >= 0 && y < horizontalBound)
			{
				if(map[x][y] != 1)
				{
					Node n1 = new Node(x,y);
					if(!close.contains(n1) && !open.contains(n1))
					{
						//G值
						if(n.x != ax && n.y != ay)
							n1.G = n.G + 14;
						else
							n1.G = n.G + 10;
						n1.H = Math.abs(n1.x - n.x) + Math.abs(n1.y - n.y);//H值
						n1.F = n1.G + n1.H;//F值
						n1.parent = n;
						list.add(n1);
					}
				}
			}
		}
		int size = list.size();
		Node[] ns = new Node[size];
		for(int i = 0 ;i < size ; i++)
		{
			ns[i] = list.get(i);
		}
		return ns;
	}
	
	
	
	public void beginFind()
	{
		cur = start;
		open.add(start);
		map[cur.x][cur.y] = 2;
		while(!open.contains(exit))
		{
			Node[] ns = FindAdjs(cur);
			for(int i = 0; i < ns.length; i++)
				open.add(ns[i]);
			open.remove(cur);
			next = open.poll();
			close.add(cur);
			cur = next;
			if(cur.x == exit.x && cur.y == exit.y)
				exit = cur;
		}
		exit.parent = cur;
		
		Node n = exit;
		while(n.parent != null)
		{
			map[n.x][n.y] = 2;
			n = n.parent;
		}
	}
	
	public void print()
	{
		for(int i  =0 ; i < map.length; i++)
		{
			for(int j = 0; j < map[i].length ; j ++)
			{
				System.out.print(map[i][j] + "   ");
			}
			System.out.println();
		}
		System.out.println("================================");
	}
	
	
	public static void main(String[] args) {
		AStar star = new AStar();
		star.beginFind();
		star.print();
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值