A*算法学习——BFS最短路径

背景说明

对地图进行建模,用二维的数组来对地图进行抽象表示。0表示障碍物,start-point和end-point可以自己指定。
具体见我的gitHub
代码如下

package graph;

import java.util.LinkedList;
import java.util.Queue;

public class SimplestBFS implements PathSearcher{

    class Node{
        int number;
        int x;
        int y;
        Node parent;
        Node (int x, int y, int number){
            this.number = number;
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString(){
            return "["+x+","+y+"]="+graph[x][y];
        }
    }

    public final int[][] graph;
    public final int barrier = 0;
    public boolean[][] visited;
    public final int[][] neighbors = {{1,0},{-1,0},{0,1},{0,-1}};
    public SimplestBFS(int[][] graph){
        this.graph = graph;
        this.visited = new boolean[graph.length][graph[0].length];
    }

    public Node getStart(int start){
        for (int i = 0; i < graph.length; i++){
            for (int j = 0; j < graph[0].length; j++){
                if (graph[i][j] == start) {
                    return new Node(i, j, start);
                }
            }
        }
        return null;
    }

    @Override
    public void search(int start, int end){
        Queue<Node> queue = new LinkedList<>();
        Node startNode = getStart(start);
        if (startNode == null) {
            System.out.println("start point does not exist!");
            System.exit(1);
        }
        //没问题
        queue.offer(startNode);
        visited[startNode.x][startNode.y] = true;
        while (!queue.isEmpty()){
            Node pNode = queue.poll();
            if (pNode.number == end){
                printPaths(pNode);
                break;
            }
            for (int i = 0; i < 4; i++){
                int nextX = pNode.x + neighbors[i][0];
                int nextY = pNode.y + neighbors[i][1];
                if (isOutOfBounds(nextX, nextY) || visited[nextX][nextY] || isBarrier(nextX,nextY)){
                    continue;
                }
                visited[nextX][nextY] = true;
                Node nextNode = new Node(nextX, nextY, graph[nextX][nextY]);
                nextNode.parent = pNode;
                queue.offer(nextNode);
            }
        }
    }

    public void printPaths(Node pNode){
        while (pNode != null){
            System.out.println(pNode);
            pNode = pNode.parent;
        }
    }

    public boolean isOutOfBounds(int x, int y){
        if (x < 0 || x >= graph.length || y < 0 || y >= graph[0].length){
            return true;
        }
        return false;
    }

    public boolean isBarrier(int x, int y){
        if (graph[x][y] == barrier){
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        int[][] graph = {
                {2,0,2,2},
                {2,1,2,0},
                {2,0,2,2},
                {2,2,2,3}
        };
        SimplestBFS bfs = new SimplestBFS(graph);
        bfs.search(1,3);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值