路径规划A*算法 JAVA实现

本文介绍了A*路径搜索算法的基本原理,并展示了如何在JAVA中实现这一经典算法,通过结合实际代价G和预估代价H来寻找最优路径。
摘要由CSDN通过智能技术生成

A*是路径搜索中最受欢迎的选择:

 

路径代价的估算: F = G+H

package com.bit.pathplanning;
/**
 *  地图网格节点类
 * @author Hao
 *
 */
public class MapNode {
	private double x;
	private double y;
	private double G;
	private double F;
	private double H;
	private MapNode fatherNode;
	private boolean reachable;
	public MapNode(){
	}
	
	public MapNode(double x,double y){
		this.x = x;
		this.y = y;
		G = 0;
		F = 0;
		H = 0;
		fatherNode = null;
		reachable = true;
	}
 
	@Override
	public String toString() {
		return "MapNode [x=" + x + ", y=" + y + ", reachable=" + reachable + "]";
	}
 
	public double getX() {
		return x;
	}
 
	public double getY() {
		return y;
	}
 
	public boolean isReachable() {
		return reachable;
	}
 
	public void setReachable(boolean reachable) {
		this.reachable = reachable;
	}
 
	public MapNode getFatherNode() {
		return fatherNode;
	}
 
	public void setFatherNode(MapNode fatherNode) {
		this.fatherNode = fatherNode;
		this.F = fatherNode.F + this.distanceTo(fatherNode);
		this.G = this.F + this.H;
	}
	
	/**
	 * 计算H值
	 */
	public double calH(MapNode fatherNode){
		double distance = 0.0;
		if(fatherNode.x == this.x)
			distance = Math.abs(fatherNode.y - this.y);
		else if(fatherNode.y == this.y)
			distance = Math.abs(fatherNode.x - this.x);
		else
			distance = 1.414*Math.abs(fatherNode.y - this.y);
		return fatherNode.H + distance;
	}
	
	public double distanceTo(MapNode to){
		return Math.sqrt((x-to.x)*(x-to.x) + (y-to.y)*(y-to.y));
	}
 
	
	public void setH(MapNode node){
		H = this.distanceTo(node);
	}
	
	public double getH() {
		return H;
	}
 
	public double getG() {
		return G;
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(x);
		result = prime * result + (int) (temp ^ (temp >>> 32));
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值