标签:
几天以来的瞎折腾,能算入门么?
漫漫几千里,A星算法这么走。
测试程序:
public class AStarTest {
/**
* @param args
*/
public static void main(String[] args) {
int[][] mySearchArea = new int[][] {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1}
};
AStar astar = new AStar(mySearchArea, 8, 6);
System.out.println("Pathfinding by A*...");
long st = new Date().getTime();
List<AStarNode> list = astar.find(2, 2, 2, 6);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
mySearchArea[list.get(i).getX()][list.get(i).getY()] = AStarConstants.NOTE_ONPATH;
}
System.out.println("--------------->Y");
System.out.println(" 012345678901234Y");
for (int i = 0; i < mySearchArea.length; i++) {
System.out.print(i % 10);
for (int j = 0; j < mySearchArea[0].length; j++) {
if (mySearchArea[i][j] == AStarConstants.NOTE_ONPATH) {
System.out.print("*");
} else {
if (mySearchArea[i][j] == AStarConstants.NOTE_UNWALKABLE) {
System.out.print("=");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
} else {
System.out.println("没有通路。");
}
System.out.println("X------------------");
System.out.println("end 用时:" + (new Date().getTime() - st));
}
}
结果:
Pathfinding by A*...
--------------->Y
012345678901234Y
0
1 =
2 * = *
3 *= *
4 ***
5
X------------------
end 用时:15
今后的预定
1.在实际中结合图,进行泛化。
2.实现A*的嵌套搜索。
先遐想一番吧。憧憬总是那么的美好。
标签:
原文地址:http://my.oschina.net/u/660460/blog/466202