Loading

BFS实现迷宫问题

BFS实现迷宫问题

问题描述,要求从起点走到终点,找出最短的距离,要避开障碍

输入描述,输入一个二维数组表示地图,其中等于10就是终点,等于-10就是起点,等于1就是障碍,等于0就是可以走的

代码:

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

/**
 * @author xuziao
 * @date 2021/10/17 19:40
 */

public class BFS {
    public static int getShort(int[][] map){
        int ans = 0;
        int startX = 0;
        int startY = 0;
        int maxX = map.length - 1;
        int maxY = map[0].length - 1;

        Queue<Coordinate> coordinateQueue = new LinkedList<>();

        for (int x = 0; x < map.length; x++) {
            for (int y = 0; y < map[0].length; y++){
                if (map[x][y] == -10) {
                    startX = x;
                    startY = y;
                }
            }
        }

        MovePoint movePoint = new MovePoint(map, maxX, maxY);

        Coordinate coordinate = new Coordinate(startX, startY, 0);
        coordinateQueue.add(coordinate);

        while (true) {
            if (coordinateQueue.isEmpty()) {
                break;
            }
            Coordinate point = coordinateQueue.poll();
            movePoint.setCoordinate(point);
            Coordinate l = movePoint.left();
            Coordinate b = movePoint.bottom();
            Coordinate t = movePoint.top();
            Coordinate r = movePoint.right();
            if (l != null) {
                if (isAns(l, map)) {
                    ans = l.step;
                    break;
                }
                coordinateQueue.add(l);
            }
            if (b != null) {
                if (isAns(b, map)) {
                    ans = b.step;
                    break;
                }
                coordinateQueue.add(b);
            }
            if (t != null) {
                if (isAns(t, map)) {
                    ans = t.step;
                    break;
                }
                coordinateQueue.add(t);
            }
            if (r != null) {
                if (isAns(r, map)) {
                    ans = r.step;
                    break;
                }
                coordinateQueue.add(r);
            }
        }
        return ans;
    }

    private static boolean isAns(Coordinate coordinate, int[][] map) {
        return map[coordinate.getX()][coordinate.getY()] == 10;
    }

    public static void main(String[] args) {
        //测试数据,-10代表起点,10代表终点,1代表墙
        int[][] data = {{-10, 0, 0, 0}, {0, 1, 0, 1}, {0, 0, 1, 10}, {1, 0, 0, 0}};
        System.out.println(isAns(new Coordinate(2, 3, 0), data));
        //start:(0, 0) end(2, 3)
    }
}



class Coordinate {
    int x;
    int y;
    int step = 0;
    public Coordinate(int x, int y, int step) {
        this.x = x;
        this.y = y;
        this.step = step;
    }

    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
}

class MovePoint{
    Coordinate coordinate;
    private final int maxX;
    private final int maxY;
    private final int[][] map;
    MovePoint(int[][] map, int maxX, int maxY) {
        this.maxY = maxY;
        this.maxX = maxX;
        this.map = map;
    }

    public void setCoordinate(Coordinate coordinate) {
        this.coordinate = coordinate;
    }

    public Coordinate top() {
        int x = coordinate.getX() - 1;
        int y = coordinate.getY();
        //判断点是否超出边界
        boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
        if (isLegal) {
            return null;
        } else {
            map [x][y] = map[x][y] == 10 ? 10 : 1;
            coordinate.step++;
            return new Coordinate(x, y, coordinate.step);
        }
    }

    public Coordinate bottom() {
        int x = coordinate.getX() + 1;
        int y = coordinate.getY();
        //判断点是否超出边界
        boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
        if (isLegal) {
            return null;
        } else {
            map[x][y] = 1;
            coordinate.step++;
            return new Coordinate(x, y, coordinate.step);
        }
    }

    public Coordinate right() {
        int x = coordinate.getX();
        int y = coordinate.getY() + 1;
        //判断点是否超出边界
        boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
        if (isLegal) {
            return null;
        } else {
            map[x][y] = 1;
            coordinate.step++;
            return new Coordinate(x, y, coordinate.step);
        }
    }

    public Coordinate left() {
        int x = coordinate.getX();
        int y = coordinate.getY() - 1;
        //判断点是否超出边界
        boolean isLegal = x > maxX || x < 0 || y > maxY || y < 0 || map[x][y] == 1;
        if (isLegal) {
            return null;
        } else {
            map[x][y] = 1;
            coordinate.step++;
            return new Coordinate(x, y, coordinate.step);
        }
    }

}

posted @ 2021-10-19 16:06  [X_O]  阅读(36)  评论(0编辑  收藏  举报