A*算法相比其他算法来说,特点是具有方向性!
如何具有方向性呢,这就有关它的代价函数了,它的代价函数分为当前代价和预估代价
当前代价 即起点到当前格子已经走了多少步
预估代价:表示从当前方块到目的地方块大概需要走多少步,并不是精确的数值,一般用于参考指导算法去优先搜索更有希望的路径。
最常用到的预估代价有欧几里德距离,即两点之间的直线距离,还有便是最简单的曼哈顿距离,也就是两点在水平方向上的总和。
在当前位置,将当前代价和预估代价相加,所得代价最低的即位下一步的最优解。
普通A*算法,八个方向
public class AStarAlgorithm {
private static final int[][] DIREC = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1},
{1, 0}, {1, -1}, {0, -1}, {-1, -1}};
static double temp_x = 0;
static double temp_y = 0;
static double result = 0;
static List<List<Integer>> list_fin;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("please enter (rows cols x1 y1 x2 y2): ");
final int rows = scanner.nextInt();
final int cols = scanner.nextInt();
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
scanner.close();
// generate a two-dimension array filled with 0
int map[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
int tmp[] = new int[cols];
Arrays.fill(tmp, 0);
map[i] = tmp;
}
int midr = rows / 2;
int midc = cols / 2;
/*map[midr - 1][midc] = 1;
map[midr][midc] = 1;
map[midr + 1][midc] = 1;*/
// for (int i = 1; i < rows - 1; i++) {
// map[i][midc] = 1;
// }
map[2][6] = 1;
map[3][6] = 1;
map[4][6] = 1;
map[5][6] = 1;
map[6][6] = 1;
findPath(map, x1, y1, x2, y2);
System.out.println(result);
}
private static void findPath(int[][] map, int x1, int y1, int x2, int y2) {
List<Position> openList = new ArrayList<Position>();
List<Position> closeList = new ArrayList<AStarAlgorithm.Position>();
boolean findFlag = false;
Position termPos = null;
// 起始点
Position startPos = new Position(x1, y1, calcH(x1, y1, x2, y2));
openList.add(startPos);
do {
// 通过在开启列表中找到F值最小的点作为当前点
Position currentPos = openList.get(0);
for (int i = 0; i < openList.size(); i++) {
if (currentPos.F > openList.get(i).F) {
currentPos = openList.get(i);
}
}
// 将找到的当前点放到关闭列表中,并从开启列表中删除
closeList.add(currentPos);
openList.remove(currentPos);
//遍历当前点对应的8个相邻点
for (int i = 0; i < DIREC.length; i++) {
//8个方向都去试了一遍
int tmpX = currentPos.row + DIREC[i][0];
int tmpY = currentPos.col + DIREC[i][1];
if (tmpX < 0 || tmpX >= map.length || tmpY < 0 || tmpY >= map[0].length) {
continue;
}
//创建对应的点
Position tmpPos = new Position(tmpX, tmpY, calcH(tmpX, tmpY, x2, y2), currentPos);
//map中对应的格子中的值为1(障碍), 或对应的点已经在关闭列表中
if (map[tmpX][tmpY] == 1 || closeList.contains(tmpPos)) {
continue;
}
//如果不在开启列表中,则加入到开启列表
if (!openList.contains(tmpPos)) {
openList.add(tmpPos);
} else {
// 如果已经存在开启列表中,则用G值考察新的路径是否更好,如果该路径更好,则把父节点改成当前格并从新计算FGH
Position prePos = null;
for (Position pos : openList) {
if (pos.row == tmpX && pos.col == tmpY) {
prePos = pos;
break;
}
}
if (tmpPos.G < prePos.G) {
prePos.setFaPos(currentPos);
}
}
}
// 判断终点是否在开启列表中
for (Position tpos : openList) {
if (tpos.row == x2 && tpos.col == y2) {
termPos = tpos;
findFlag = true;
break;
}
}
} while (openList.size() != 0);
if (!findFlag) {
System.out.println("no valid path!");
return;
}
Stack<String> resStack = new Stack<String>();
String pattern = "(%d, %d)";
if (termPos != null) {
resStack.push(String.format(pattern, termPos.row, termPos.col));
temp_x = termPos.row;
temp_y = termPos.col;
int temp_fangxiang = 0;
while (termPos.fa != null) {
termPos = termPos.fa;
// System.out.println(termPos.row);
// System.out.println(termPos.col);
double value = Math.sqrt(Math.pow(Math.abs(termPos.row - temp_x), 2) + Math.pow(Math.abs(termPos.col - temp_y), 2));
// System.out.println(value);
result = result + Math.sqrt(Math.pow(Math.abs(termPos.row - temp_x), 2) + Math.pow(Math.abs(termPos.col - temp_y), 2));
// System.out.println(result);
temp_x=termPos.row;
temp_y=termPos.col;
resStack.push(String.format(pattern, termPos.row, termPos.col));
}
}
StringBuilder sb = new StringBuilder();
while (!resStack.empty()) {
sb.append(resStack.pop());
if (!resStack.empty()) {
sb.append(" -> ");
}
}
System.out.println(sb.toString());
}
/**
* 计算某个格子的H值
*
* @param x
* @param y
* @param tx 终点的x值
* @param ty 终点的y值
* @return
*/
private static int calcH(int x, int y, int tx, int ty) {
//曼哈顿代价公式 预估代价
int diff = Math.abs(x - tx) + Math.abs(y - ty);
return diff * 10;
}
static class Position {
public int F;
public int G;
public int H;
public Position fa;
public int row;
public int col;
public Position() {
}
public Position(int row, int col, int H) {
this(row, col, H, null);
}
public Position(int row, int col, int H, Position pos) {
this.H = H;
this.row = row;
this.col = col;
this.fa = pos;
this.G = calcG();
this.F = G + H;
}
/**
* 计算某个点到起始点的代价G
*
* @return
*/
private int calcG() {
if (fa == null) return 0;
if (fa.row != this.row && fa.col != this.col) {
return 14 + fa.G;
}
return 10 + fa.G;
}
public void setFaPos(Position pos) {
this.fa = pos;
this.G = calcG();
this.F = G + H;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(obj instanceof Position)) {
return false;
}
Position pos = (Position) obj;
return this.row == pos.row && this.col == pos.col;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + row;
result = prime * result + col;
return result;
}
}
}