資源簡介
有些許改進,優化了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
評論
共有 條評論