算法起步之A星算法

算法起步之A星算法
原文: 算法起步之A星算法

用途:

       寻找最短路径,优于bfs跟dfs

描述:

       基本描述是,在深度优先搜索的基础上,增加了一个启发式算法,在选择节点的过程中,不是盲目选择,而是有目的的选的,F=G+H,f(n)=g(n)+h(n)

       g(n)是当前节点到开始节点的花费h(n)是当前节点到结束节点的花费f(n)是衡量一个衡量标准

       最核心的是h(n)的估算,这里用到了启发式算法,且h(n)=

资料:

http://www.java3z.com/cwbwebhome/article/article2/2825.html的flash做的很形象

http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx讲的很详细

http://developer.51cto.com/art/201112/307973.htmjava版代码

代码:

package com.langsin.gsc.acm.astar;
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 open;
//  close列表
    private List 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);
       Noderesult=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[i][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=newArrayList();
       close=newArrayList();
    }
    /**
     * 开始节点的坐标跟终点节点的坐标
     * @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);
    }
    /**
     * 分八个方向开始检查结点
     * @param bNode
     * @param eNode
     */
    private void searchnode(NodebNode,Node eNode){
//     将开始节点加入到open中
       open.add(bNode);
//     当open列表中有节点时就循环
       while (open.size()>0) {
//         取出第一个节点设置成当前节点开始检查他的八个方向
           Nodenownode=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=0) {
              checkNode(nownode.getX(),nownode.getY()-1, nownode, COST_ZHI);
           }
//         向右
           if (nownode.getY()+1=0&&nownode.getY()-1>=0) {
              checkNode(nownode.getX()-1,nownode.getY()-1, nownode, COSR_XIE);
           }
//         右上
           if(nownode.getX()-1>=0&&nownode.getY()+1=0){
              checkNode(nownode.getX()+1,nownode.getY()-1, nownode, COSR_XIE);
           }
//         右下
           if (nownode.getX()+1 list,int x,inty){
       int key=-1;
       for (int i = 0; i {
    @Override
    public int compare(Node o1, Nodeo2) {
       return o1.getF()-o2.getF();
    }
}

posted on 2014-02-23 15:55 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/3562027.html

你可能感兴趣的:(算法起步之A星算法)