用Astar(A*)算法 实现的迷宫最短路。代码比较规范,可作为模板

105 篇文章 1 订阅
21 篇文章 0 订阅
转载自: http://www.cnblogs.com/kdy71107216/archive/2010/08/06/1794447.html
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main {
    boolean graph[][];
    int tableTwo[][];
    int path[][] = {{-1,0},{1,0},{0,1},{0,-1}};
    int sx,sy,tx,ty,n,m;
    PriorityQueue tableOne;
    
    private Scanner reader = null;
    public int solve(){
        readInput();
        init();
        return aStar();
    }
    
    private boolean isAnswer(State state){
        return state.x == tx && state.y == ty;
    }
    
    State step(State state, int dir){
        int dirx = state.x + path[dir][0];
        int diry = state.y + path[dir][1];
        if(dirx >= n || dirx < 0 || diry >= m || diry < 0)
            return null;
        if(!graph[dirx][diry])
            return null;
        State newState = new State();
        newState.x = dirx;
        newState.y = diry;
        return newState;
    }
    
    private void expand(State state){
        for(int i = 0;i < path.length;i++){
            State newState = step(state,i);
            if(newState == null) continue;
            newState.h = calc(newState);
            newState.g = state.g + 1;
            newState.f = newState.h + newState.g;
            if(insertIntoTableTwo(newState))
                insertIntoTableOne(newState);
        }
    }
    
    private void insertIntoTableOne(State state){
        tableOne.add(state);
    }
    
    private boolean insertIntoTableTwo(State state){
        if(tableTwo[state.x][state.y] <= state.f)
            return false;
        tableTwo[state.x][state.y] = state.f;
        return true;
    }
    
    private int aStar(){
        boolean ok = false;
        while(!ok && ! tableOne.isEmpty()){
            State tmp = (State)tableOne.poll();
            if(isAnswer(tmp)){
                return tmp.f;
            }else {
                expand(tmp);
            }
        }
        return -1;
    }
    public class StateComparator implements Comparator{
        public int compare(Object arg0, Object arg1) {
            State s1 = (State)arg0;
            State s2 = (State)arg1;
            return s1.f - s2.f;
        }
        
    }
    public class State{
        public int x;
        public int y;
        public int f;
        public int g;
        public int h;
    };
    int calc(State state){
        return (int)Math.sqrt(Math.pow(state.x - tx, 2) + Math.pow(state.y - ty,2));
    }
    void init(){
        reader = new Scanner(System.in);
        n = reader.nextInt();
        m = reader.nextInt();
        reader.nextLine();
        graph = new boolean [n][m];
        tableTwo = new int [n][m];
        tableOne = new PriorityQueue(100000, new StateComparator());
        readInput();
        for(int i = 0;i < tableTwo.length;i++){
            for(int j = 0;j < tableTwo[0].length;j++){
                tableTwo[i][j] = Integer.MAX_VALUE;
            }
        }
        State newState = new State();
        newState.x = sx;
        newState.y = sy;
        newState.g = 0;
        newState.h = calc(newState);
        newState.f = newState.g + newState.h;
        tableOne.add(newState);
        tableTwo[sx][sy] = newState.f;
    }
    void readInput(){
        for(int i = 0;i < n;i++){
            String buf = reader.nextLine();
            for(int j = 0;j < m;j++){
                if(buf.charAt(j) == '.'){
                    graph[i][j] = true;
                } else if(buf.charAt(j) == '*'){
                    graph[i][j] = false;
                } else if(buf.charAt(j) == 'S'){
                    graph[i][j] = true;
                    sx = i; sy = j;
                } else if(buf.charAt(j) == 'T'){
                    graph[i][j] = true;
                    tx = i; ty = j;
                }
            }
        }
    }
    
    public static void main(String [] args){
        Main maze = new Main();
        maze.init();
        System.out.println(maze.aStar());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值