资源简介
A*源码免费开放,内涵main函数可执行测试之用。
沈阳-亚冰
代码片段和文件信息
package com.liuybc.astar;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AStarMap {
private List openList = new ArrayList();
private Map closeMap = new HashMap();
// private Set< AStarNode> closeSet = new HashSet< AStarNode>();
private boolean isFind = false;
private List path = new ArrayList();
/** 障碍物 */
public static final int STATE_BARRIER = 2;
AStarNode target;
AStarNode source;
int[][] astarData;
public AStarMap(int xGridNum int yGridNum) {
astarData = new int[yGridNum][xGridNum];
source = new AStarNode(0 0);
target = new AStarNode(xGridNum - 1 yGridNum - 1);
}
private int[][]data;
public int[][] getData() {
return data;
}
public void setData(int[][] data) {
this.data = data;
}
// public static void main(String[] args) {
// AStarMap asm = new AStarMap(100 200);
// // int[][]data= {
// // {00000}{00000}{00100}{00000}{00000}
// // {00000}{00000}{00100}{00000}{00000}
// // {00000}{00000}{00100}{00000}{00000}
// // {00000}{00100}{00100}{00000}{00000}
// // {00000}{00000}{00000}{00000}{00000}
// // };
//
// asm.loadData(data 1 -1);
// List list1 = asm.find();
//
// for (int i = 0; i < list1.size(); i++) {
// AStarNode asn = (AStarNode) list1.get(i);
// System.out.println(asn.getX() + “ “ + asn.getY());
// }
// asm.toString();
// List list = asm.getOpenList();
// for (int i = 0; i < list.size(); i++) {
// AStarNode asn = (AStarNode) list.get(i);
// System.out.println(asn.getX() + “ “ + asn.getY()
// + “ ====== “ + asn.getFather().getX() + “ “
// + asn.getFather().getY());
// }
//
// }
public AStarNode getTarget() {
return target;
}
public AStarNode getSource() {
return source;
}
public int[][] getAStarData() {
return astarData;
}
/**
* 搜索算法
*/
public List find() {
init();
AStarNode current = null;
while (!isEnd() && !isFind()) {
current = getMinFNodeFromOpenList();
if (isAchieve(current)) { // 是否以及找到目标
buildPath(current);
isFind = true;
} else {
add2CloseMap(current);
for (AStarNode neighbor : getNeighbor(current)) {
if (neighbor == null || isInCloseMap(neighbor) || /*
* 如果已经在关闭列表中,
* 那么跳过
*/
isCanNotGo(current neighbor))
continue;
boolean isBetter = true;
AStarNode nodeFromOpenList = getNodeFromOpenList(neighbor);
if (nodeFromOpenList != null) { // 如果在开启列表中
neighbor = nodeFromOpenList;
int tg = neighbor.distinctG(current);
isBetter = tg > neighbor.getG() ? false : true;
} else {
add2OpenList(neighbor);
}
if (isBetter) {
neighbor.reCalculatorGAndH(current target);
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-09-13 15:58 astar\
文件 8592 2012-09-13 16:00 astar\AStarMap.java
文件 3272 2012-09-12 14:37 astar\AStarNode.java
评论
共有 条评论