资源简介
有些许改进,优化了A*算法和机器人行进方式
代码片段和文件信息
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class AStarAlgorithm {
private int[][] map;//获取静态地图
private List openList;//开启列表
private List closeList;//关闭列表
private final int COST_STRAIGHT = 10;//路径评分
private int row;//行
private int column;//列
public AStarAlgorithm (int[][] mapint rowint column){
this.map=map;
this.row=row;
this.column=column;
openList=new ArrayList();
closeList=new ArrayList();
}
//查找路径
public int search(int x1int y1int x2int y2){
if(x1<0||x1>=column||x2<0||x2>=column||y1<0||y1>=row||y2<0||y2>=row){
return -1;
}
if(map[x1][y1]==0||map[x2][y2]==0){
return -1; //地图错误
}
Node sNode=new Node(x1y1null);
Node eNode=new Node(x2y2null);
openList.add(sNode);
List resultList=search(sNode eNode);
if(resultList.size()==0){
return 0; //没有找到路径
}
for(Node node:resultList){
map[node.getX()][node.getY()]=-1;
}
System.out.println(“路径长度:“+resultList.size());
return 1; //找到坐标
}
//节点查找,核心算法
private List search(Node sNodeNode eNode){
List resultList=new ArrayList();
boolean isFind=false;
Node node=null;
while(openList.size()>0){
//取出openlist中F值最低的,用于下一个查找节点
node=openList.get(0);
//判断此节点是否为终点
if(node.getX()==eNode.getX()&&node.getY()==eNode.getY()){
isFind=true;
break;
}
//查找上一个
if((node.getY()-1)>=0){
checkPath(node.getX()node.getY()-1node eNode COST_STRAIGHT);
}
//下
if((node.getY()+1)
checkPath(node.getX()node.getY()+1node eNode COST_STRAIGHT);
}
//左边
if((node.getX()-1)>=0){
checkPath(node.getX()-1node.getY()node eNode COST_STRAIGHT);
}
//右边
if((node.getX()+1) checkPath(node.getX()+1node.getY()node eNode COST_STRAIGHT);
}
//右下
if((node.getX()+1) checkPath(node.getX()+1node.getY()+1node eNode COST_STRAIGHT);
}
//左下
if((node.getX()-1)>=0&&(node.getY()+1)
checkPath(node.getX()-1node.getY()+1node eNode COST_STRAIGHT);
}
//右上
if((node.getX()+1)=0){
checkPath(node.getX()+1node.getY()-1node eNode COST_STRAIGHT);
}
//左上
if((node.getX()-1)>=0&&(node.getY()-1)>=0){
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 412 2017-01-03 16:10 Robot2\.classpath
文件 382 2017-01-01 14:09 Robot2\.project
目录 0 2017-01-03 16:12 Robot2\.settings\
文件 598 2017-01-01 14:09 Robot2\.settings\org.eclipse.jdt.core.prefs
目录 0 2017-01-03 16:12 Robot2\bin\
文件 4731 2017-01-03 16:10 Robot2\bin\AStarAlgorithm.class
文件 2559 2017-01-04 08:16 Robot2\bin\AvoidBlock.class
文件 2407 2017-01-03 16:10 Robot2\bin\MyEnv.class
文件 1434 2017-01-03 16:10 Robot2\bin\Node.class
文件 712 2017-01-03 16:10 Robot2\bin\NodeFComparator.class
文件 4701 2017-01-04 08:20 Robot2\bin\Robot.class
目录 0 2017-01-03 16:12 Robot2\lib\
文件 94136 2016-12-26 09:40 Robot2\lib\simbad-1.4.jar
文件 318956 2016-12-27 19:31 Robot2\lib\vecmath.jar
目录 0 2017-01-03 16:12 Robot2\src\
文件 5933 2017-01-03 09:13 Robot2\src\AStarAlgorithm.java
文件 1495 2017-01-04 08:16 Robot2\src\AvoidBlock.java
文件 1950 2017-01-01 15:13 Robot2\src\MyEnv.java
文件 1128 2017-01-02 09:22 Robot2\src\Node.java
文件 317 2017-01-02 09:32 Robot2\src\NodeFComparator.java
文件 12175 2017-01-04 08:20 Robot2\src\Robot.java
- 上一篇:什么是极限学习机
- 下一篇:wiki.zh.text.model
评论
共有 条评论