A*算法的JAVA实现

UFOFox 2005-12-09 12:05:48
最短路径的算法大家有有没有写过?
我用Vector实现了一个,发现效率不是很高,有谁写过类似的代码?希望效率高一些
要求用2D数组,能够找出任意两点之间的最短路径,能够绕过障碍~~~,总之,谢谢各位:)
...全文
359 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
UFOFox 2005-12-11
  • 打赏
  • 举报
回复
其实有时候不需要最短路径的~~~,不过有时候需要,需要实践,谢谢各位:)
cxc014 2005-12-11
  • 打赏
  • 举报
回复
关键还是要看你的最短路径是早已计算好了的,还是在进行中动态来算的?
Timium 2005-12-11
  • 打赏
  • 举报
回复
/*************************** Part 2 ************************/

private byte calculateH(byte y, byte x) {
return (byte) (ORTHOGONAL_COST * (Math.abs(this.lEnd.Y - y) +
Math.abs(this.lEnd.X - x)));
}

public void openListInsert(byte tY, byte tX, byte g, Square parent) {
this.squares[tY][tX] = new Square(tY, tX, g, calculateH(tY, tX));
this.squares[tY][tX].parent = parent;
heapInsert(this.squares[tY][tX]);
this.map[tY][tX] |= OPEN_MASK;
}

public Square openListRemove() {
Square temp = heapDeleteMin();
this.map[temp.Y][temp.X] |= OPEN_MASK;
this.squares[temp.Y][temp.X] = null;
return temp;
}

//Binary Heap
public void heapPercolateUp(int position) {
if (position <= this.heapSize) {
while (position > 1) {
if (this.heapTree[position].f < this.heapTree[position / 2].f) {
Square temp = this.heapTree[position];
this.heapTree[position / 2].position = position;
this.heapTree[position] = this.heapTree[position / 2];
temp.position = position / 2;
this.heapTree[position / 2] = temp;
position = position / 2;
}
else return;
}
}
}

public boolean heapInsert(Square element) {
if (this.heapSize == heapTree.length - 1 || element == null) {
return false;
}

this.heapSize++;
int hole = this.heapSize;

while (hole > 1 && element.f < this.heapTree[hole / 2].f) {
this.heapTree[hole / 2].position = hole;
this.heapTree[hole] = this.heapTree[hole / 2];
hole = hole / 2;
}

this.heapTree[hole] = element;
this.heapTree[hole].position = hole;

return true;
}

public Square heapDeleteMin() {
if (this.heapSize == 0) {
return null;
}

Square smallest = this.heapTree[1];
this.heapTree[this.heapSize].position = 1;
this.heapTree[1] = this.heapTree[this.heapSize];
this.heapTree[this.heapSize] = null;
this.heapSize--;

int current = 1;
int child;
while (2 * current <= this.heapSize) {
child = 2 * current;

if (child != this.heapSize &&
this.heapTree[child].f > this.heapTree[child + 1].f) {
child++;
}

if (this.heapTree[current].f > this.heapTree[child].f) {
Square temp = this.heapTree[current];
this.heapTree[child].position = current;
this.heapTree[current] = this.heapTree[child];
temp.position = child;
this.heapTree[child] = temp;
}
else {
break;
}
current = child;
}

return smallest;
}

}
class Square {
public byte f;
public byte g;
public byte h;

public int position; //heap position

public byte Y;
public byte X;

public Square parent;

public Square (byte y, byte x, byte g, byte h) {
this.Y = y;
this.X = x;

if ( !(g == 0 && h == 0) ) {
this.g = g;
this.h = h;
this.f = (byte)(this.g + this.h);
}
}

public void setG(byte g) {
this.g = g;
this.f = (byte)(this.g + this.h);
}
}
/*************************** Part 2 ****************************/
Timium 2005-12-11
  • 打赏
  • 举报
回复
以前用过的。map数据的格式根据你的需要改一下。
太长了,只能分开发
/*********************** Part 1 *****************************/
/**
* AStar pathfinding algorithm
*/
public class AStar {
private Square[][] squares;

public static final byte WALL = 0x1, BLANK = 0x0;
public static final byte WALL_MASK = (byte) 0xf;
public static final byte OPEN_MASK = (byte) 0x80;
public static final byte CLOSED_MASK = (byte) 0x40;

private byte[][] map;

private Square lStart;
private Square lEnd;

private static final byte ORTHOGONAL_COST = 1;

byte height;
byte width;

//Binary Heap
public Square[] heapTree;
public int heapSize;

boolean first = true;

void updateMap(byte[][] mapMatrix) {
if( map != null ){
map = null;
releaseFind();
}else{
lStart = new Square((byte) 0, (byte) 0, (byte) 0, (byte) 0);
lEnd = new Square((byte) 0, (byte) 0, (byte) 0, (byte) 0);

heapTree = new Square[height * width + 1];
squares = new Square[height][width];
}
map = mapMatrix;
}

public void releaseFind() {
int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
squares[i][j] = null;
}
}

for (i = 0; i < heapTree.length; i++) {
heapTree[i] = null;
}
}

public Square findPath(byte sy, byte sx, byte ey, byte ex, boolean canfly) {
lStart.X = sx;
lStart.Y = sy;
lEnd.X = ex;
lEnd.Y = ey;
if( canfly ) {
Square sqr, last;
last = lStart;
int sign;
if( ex != sx ){
sign = (ex - sx) / Math.abs(ex - sx);
for (byte i = (byte) (sx + sign); i != ex; i += sign) {
sqr = new Square(sy, i, (byte) 0, (byte) 0);
sqr.parent = last;
last = sqr;
}
}
if( ey != sy ){
sign = (ey - sy) / Math.abs(ey - sy);
for (byte i = (byte) (sy); i != ey; i += sign) {
sqr = new Square(i, ex, (byte) 0, (byte) 0);
sqr.parent = last;
last = sqr;
}
}
lEnd.parent = last;
return lEnd;
}

byte height = this.height;
byte width = this.width;

int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
map[i][j] &= WALL_MASK;
}
}
heapSize = 0;

openListInsert(this.lStart.Y, this.lStart.X, (byte)0, null);
Square current = null;

byte tY = 0, tX = 0, tG = 0, tmpG;

while (heapSize != 0) {
//取出open表中第一个
current = openListRemove();

//到达终点!
if (current.Y == this.lEnd.Y && current.X == this.lEnd.X) {
return current;
}

byte cY = current.Y;
byte cX = current.X;

fl:
for (i = 0; i < 4; i++) {
switch (i) {
case 0: //上
tY = (byte)(cY - 1);
if (tY < 0)continue fl;
tX = cX;
tG = ORTHOGONAL_COST;
break;
case 1: //左
tX = (byte)(cX - 1);
if (tX < 0)continue fl;
tY = cY;
tG = ORTHOGONAL_COST;
break;
case 2: //右
tX = (byte)(cX + 1);
if (tX >= width)continue fl;
tY = cY;
tG = ORTHOGONAL_COST;
break;
case 3: //下
tY = (byte)(cY + 1);
if (tY >= height)continue fl;
tX = cX;
tG = ORTHOGONAL_COST;
break;
default:
break;
}


if ( (this.map[tY][tX] & WALL_MASK) != WALL &&
(this.map[tY][tX] & CLOSED_MASK) == 0) {
if ( (this.map[tY][tX] & OPEN_MASK) == 0) {
openListInsert(tY, tX, (byte) (current.g + tG),
current);
}
else {
tmpG = (byte) (current.g + tG);
if (this.squares[tY][tX].g > tmpG) {

this.squares[tY][tX].setG(tmpG);
this.squares[tY][tX].parent = current;
heapPercolateUp(squares[tY][tX].position);
}
}
}
}
this.map[current.Y][current.X] |= CLOSED_MASK;
}
return null;
}
/*************************** Part 1 ******************************/
miaoliujun 2005-12-10
  • 打赏
  • 举报
回复
如果用Vector你在初始化这个变量的时候要注意容量的大小一定要适合,如果在运行的时候大小发生了变化,性能肯定是会有很大的影响的
xueyong1203 2005-12-09
  • 打赏
  • 举报
回复
在手机里实现AStar太占空间
其实大多数情况下简单的寻路足矣~~
程序 = 数据结构 + 算法  程序是为了解决实际问题而存在的。然而为了解决问题,必定会使用到某些数据结构以及设计一个解决这种数据结构的算法。如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。编程实战算法,不是念PPT,我们讲的就是实战与代码实现与企业应用。程序 = 数据结构 + 算法                ——图灵奖得主,计算机科学家N.Wirth(沃斯)作为程序员,我们做机器学习也好,做python开发也好,java开发也好。有一种对所有程序员无一例外的刚需 —— 算法与数据结构日常增删改查 + 粘贴复制 + 搜索引擎可以实现很多东西。同样,这样也是没有任何竞争力的。我们只可以粘贴复制相似度极高的功能,稍复杂的逻辑没有任何办法。语言有很多,开发框架更是日新月异3个月不学就落后我们可以学习很多语言,很多框架,但招聘不会考你用5种语言10种框架实现同一个功能。真正让程序员有区分度,企业招聘万年不变的重点 —— 算法与数据结构。算法代表程序员水平的珠穆朗玛。 本视频由微软全球最有价值专家尹成录制,拒绝念PPT,代码实战数据结构与算法导论。除了传统数据结构算法,加入高并发线程安全数据结构,分布式负载均衡算法,分布式哈希表,分布式排序等等现代算法。  算法,晦涩难懂,却又是IT领域受重视的素养之一。可以说,算法能力往往决定了一个程序员能够走多远。因此,BAT/FLAG等国内外各大名企非常喜欢在面试环节考核求职者的算法编程,这也成为了无数准程序员们过不去的一道“坎”。如何入门并成为一名出色的算法工程师?但无论半路出家还是科班出身,除学生时代搞算法竞赛的同学外真正用心学习过算法与数据结构太少太少。对于后期想要学习算法与数据结构却不得不面对以下问题:没有自己的知识框架,无法关联知识点,学习效率低有疑问而无人解答,有问题无法理解全靠猜测,一个问题卡好几天市面上资料题解质量参差不齐,正确性未可知Google算法-工程师尹成大哥学习算法

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧