回 帖 发 新 帖 刷新版面

主题:A星算法中节点传值问题?

以下是A*算法。有两个疑问:一、resultNode和nownode都为节点变量,

这行代码中resultNode=nownode,nownode只是终点一个节点值而已,

为什么可以循环判断取值呢? while(result.getParentnode()!=null){...}。

二、只根据一个终节点nownode怎么求得map[][]路径。


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

/**

* A*寻路算法

* 地图中1表示可以通过,0表示不能通过

* 当结束点加到open队列中时查找结束

* gh值的设定问题。

*/

public classAstar {

//  地图

    private int[][] map;

//  行

    private int row;

//  列

    private int colum;

//  水平或竖直方向的花费

    private int COST_ZHI=10;

//  对角线方向的花费

    private int COSR_XIE=14;

//  open列表

    private List<Node> open;

//  close列表

    private List<Node> close;

//  开始节点

    private Node beginNode;

//  结束节点

    private Node endNode;

//  结构节点

    private Node resultNode=null;

    /**

     * 测试方法

     * @param args

     */

    public static void main(String[] args) {

       int[][] map = new int[][] {

              {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },

              {1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },

              {1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },

              {1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },

              {1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },

              {1, 1, 1, 1, 0, 1, 1, 1, 1, 1 }

              };

       Astarastar=newAstar(map, 6, 10);

       astar.search(0,0, 5, 9);

       Node result=astar.getResultNode();

       while(result.getParentnode()!=null){

           map[result.getX()][result.getY()]=2;

           result=result.getParentnode();

       }

       map[result.getX()][result.getY()]=2;

      

       for (int i = 0; i <6; i++) {

           for (int j = 0; j < 10; j++){

              System.out.print(" "+map[j]);

           }

           System.out.println();

       }

    }

    /**

     *  初始化地图长宽还有open跟close队列

     * @param map

     * @param row

     * @param colum

     */

    public Astar(int[][] map,int row,int colum){

       this.map=map;

       this.row=row;

       this.colum=colum;

       open=new ArrayList<Node>();

       close=new ArrayList<Node>();

    }

    /**

     * 开始节点的坐标跟终点节点的坐标

     * @param x1

     * @param y1

     * @param x2

     * @param y2

     */

    public void search(int x1,int y1,int x2,int y2) {

//     如果在节点外直接返回

       if (x1<0||x1>=row||y1<0||y1>colum||x2<0||x2>=row||y2<0||y2>colum) {

           return;

       }

//     如果无法通过直接返回

       if (map[x1][y1]==0||map[x2][y2]==0) {

           return;

       }

       beginNode=new Node(x1, y1, null);

       endNode=new Node(x2, y2, null);

//     开始按方向搜索

       searchnode(beginNode, endNode);

    }



回复列表 (共1个回复)

沙发

    /**
     * 分八个方向开始检查结点
     * @param bNode
     * @param eNode
     */
    private void searchnode(Node bNode,Node eNode){
//     将开始节点加入到open中
       open.add(bNode);
//     当open列表中有节点时就循环
       while (open.size()>0) {
//         取出第一个节点设置成当前节点开始检查他的八个方向
           Node nownode=open.get(0);
//         如果是终点则推出循环
           if (nownode.getX()==endNode.getX()&&nownode.getY()==endNode.getY()) {
//            将这个节点设置成结果节点
              resultNode=nownode;
              break;
           }
//          向上
           if(nownode.getX()-1>=0) {
              checkNode(nownode.getX()-1,nownode.getY(), nownode,COST_ZHI);
           }
//         向下
           if (nownode.getX()+1<row) {
              checkNode(nownode.getX()+1,nownode.getY(), nownode, COST_ZHI);
           }
//         向左
           if(nownode.getY()-1>=0) {
              checkNode(nownode.getX(),nownode.getY()-1, nownode, COST_ZHI);
           }
//         向右
           if (nownode.getY()+1<colum) {
               checkNode(nownode.getX(), nownode.getY()+1,nownode, COST_ZHI);
           }
//         左上
           if(nownode.getX()-1>=0&&nownode.getY()-1>=0) {
              checkNode(nownode.getX()-1,nownode.getY()-1, nownode, COSR_XIE);
           }
//         右上
           if(nownode.getX()-1>=0&&nownode.getY()+1<colum) {
              checkNode(nownode.getX()-1,nownode.getY()+1, nownode, COSR_XIE);
           }
//         左下
           if (nownode.getX()+1<row&&nownode.getY()-1>=0){
              checkNode(nownode.getX()+1,nownode.getY()-1, nownode, COSR_XIE);
           }
//         右下
           if (nownode.getX()+1<row&&nownode.getY()+1<colum) {
              checkNode(nownode.getX()+1,nownode.getY()+1, nownode, COSR_XIE);
           }
//         将open列表中的第一个取出放入到close列表中
           close.add(open.remove(0));
//         对open列表进行排序
           Collections.sort(open, new nodecompare());
       }
      
    }
    /**
     * 检查每一个结点
     * @param x
     * @param y
     * @param parentNode
     * @param cost
     */
    private void checkNode(int x,int y,Node parentNode,int cost){
//     如果无法通过则返回
       if (map[x][y]==0) {
           return ;
       }
//     如果在close中有则返回
       if (checklist(close, x, y)!=-1) {
           return;
       }
//     建立这个结点
       Node node=new Node(x, y, parentNode);
//     计算他的fgh值
       setFGH(node,cost, endNode);
       int index=-1;
//     如果open中有,若f值小则更新他否则不添加.没有的话则添加这个结点
       if ((index=checklist(open, x, y))!=-1) {
           if (node.getF()<open.get(index).getF()) {
              open.set(index, node);
           }
       }else {
           open.add(node);
       }
    }
   
    /**
     * 计算FGH值
     * @param node
     */
    private void setFGH(Node node,int cost,Node eNode){
      
       if (node.getParentnode()!=null) {
           node.setG(node.getParentnode().getG()+cost);
       }else {
           node.setG(cost);
       }
//     H值的一种计算方法,也可以采用其他方法    曼哈顿(manhattan)估价函数
       node.setH((Math.abs(node.getX()-eNode.getX())+Math.abs(node.getY()-eNode.getY()))*10);
       node.setF(node.getG()+node.getH());
      
    }
    /**
     * 检查列表中是否已经有该结点
     * @param list
     * @param x
     * @param y
     * @return
     */
    private intchecklist(List<Node> list,int x,inty){
       int key=-1;
       for (int i = 0; i <list.size(); i++) {
           if(list.get(i).getX()==x&&list.get(i).getY()==y) {
              key=i;
              break;
           }
       }
       return key;
    }
    /**
     * 放回结果路径(链表)
     * @return
     */
    public Node getResultNode(){
       return resultNode;
    }
}

/**
* 节点类。主要包括xy坐标父节点 f=g+h
*
*/
class Node{
//  坐标xy
    private int x;
    private int y;
//  父节点
    private Node parentnode;
//  F=G+H
    private int g;
    private int h;
    private int f;
//  构造函数
    public Node(int x, int y, Node parentnode) {
       super();
       this.x = x;
       this.y = y;
       this.parentnode = parentnode;
    }
    public int getX() {
       return x;
    }
    public void setX(int x) {
       this.x = x;
    }
    public int getY() {
       return y;
    }
    public void setY(int y) {
       this.y = y;
    }
    public Node getParentnode() {
       return parentnode;
    }
    public void setParentnode(Nodeparentnode) {
       this.parentnode = parentnode;
    }
    public int getG() {
       return g;
    }
    public void setG(int g) {
       this.g = g;
    }
    public int getH() {
       return h;
    }
    public void setH(int h) {
       this.h = h;
    }
    public int getF() {
       return f;
    }
    public void setF(int f) {
       this.f = f;
    }
}
/**
* 比较器用来给open队列排序
*/
class nodecompare implements Comparator<Node>{
    @Override
    public int compare(Node o1, Nodeo2) {
       return o1.getF()-o2.getF();
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册