设为首页 加入收藏

TOP

营救公主(Java实现A*算法解决迷宫问题) (四)
2014-11-24 11:07:16 】 浏览:376
Tags:营救 公主 Java 实现 算法 解决 迷宫 问题
getCost() / STRAIGHT_DISTANCE;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((position == null) 0 : position.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PositionWeight other = (PositionWeight) obj;
if (position == null)
{
if (other.position != null)
return false;
}
else
if (!position.equals(other.position))
return false;
return true;
}

@Override
public String toString()
{
return "PositionWeight [position=" + position + ", distanceOfPrince="
+ distanceOfPrince + ", distanceOfPrincess="
+ distanceOfPrincess + ", father=" + father.getPosition()
+ ", cost=" + cost + "]";
}

/**
* 设置到目标节点的距离
*/
private void countDistanceToTarget(PositionWeight target)
{
Position targetPosition = target.getPosition();
int distanceToTarget = getPosition()
.getDistanceOfTarget(targetPosition);
setDistanceOfPrincess(distanceToTarget);
}

private void setDistanceOfPrince(int distanceOfPrince)
{
this.distanceOfPrince = distanceOfPrince;
}

private int getDistanceOfPrincess()
{
return distanceOfPrincess;
}

private void setDistanceOfPrincess(int distanceOfPrincess)
{
this.distanceOfPrincess = distanceOfPrincess;
}

private void setFather(PositionWeight father)
{
this.father = father;
}

private void setCost(int cost)
{
this.cost = cost;
}
}

/**
* 位置信息的权值
*/
public class PositionWeight
{
/**
* 水平或者垂直移动一格的距离
*/
private final static int STRAIGHT_DISTANCE = 10;

/**
* 当前的位置信息
*/
private Position position;

/**
* 起始点(王子的起始位置),经由当前点的父节点后,到当前点的距离(仅包括垂直和水平直线上的)
*/
private int distanceOfPrince;

/**
* 当前点到目标点(公主位置)的距离
*/
private int distanceOfPrincess;

/**
* 父节点的权值
*/
private PositionWeight father;

/**
* 总开销:包括起始点到当前点和当前点到终点的开销之和
*/
private int cost;

public PositionWeight(Position position)
{
this.position = position;
}

public PositionWeight(Position position, PositionWeight father,
PositionWeight target)
{
this(position);
countDistanceToTarget(target);
updateByFather(father);
}

/**
* 获取父子节点间的距离:对角线为14,水平、垂直为10
*/
public int getDistanceFromAttemptFather(PositionWeight father)
{
Position fatherPosition = father.getPosition();
return fatherPosition.getOffsetOfDistance(getPosition());
}

/**
* 更新父节点,并设置当前点的权值
*/
public void updateByFather(PositionWeight father)
{
setFather(father);
int distanceOfPrince = getDistanceFromAttemptFather(father);
setDistanceOfPrince(distanceOfPrince + father.getDistanceOfPrince());
setCost(getDistanceOfPrince() + getDistanceOfPrincess());
}

public Position getPosition()
{
return position;
}

public PositionWeight getFather()
{
return father;
}

public int getCost()
{
return cost;
}

public int getDistanc

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 4/11/11
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[小代码]如何捕获应用程序日志。 下一篇Java - 通过IP地址获取用户所在地

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目