BFS广度优先寻找最短路径

工具类:

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

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class BFS {
    static int[] X = {0, 0, 1, -1};//增量数组

    static int[] Y = {1, -1, 0, 0};

    PlayCell[][] map;

    boolean[][] visited;

    Point from = new Point();

    Point to = new Point();

    public BFS(int fromRow, int fromColumn, int toRow, int toColumn, PlayCell[][] map) {
        // 标记已经走过的
        this.visited = new boolean[map.length][map[0].length];
        this.map = map;
        //起点和重点的坐标
        this.to.setX(toRow);
        this.to.setY(toColumn);
        this.from.setX(fromRow);
        this.from.setY(fromColumn);
        this.from.setStep(0);//初始化起点的层数为0,即S到S的最小步数为0
        this.from.getNodes().add(from);
    }

    //检测位置(x,y)是否有效
    Boolean test(int x, int y) {
        if (x >= map.length || x < 0 || y >= map[0].length || y < 0) { //越界
            return false;
        }
        if (map[x][y].isObstacle() || map[x][y].isRobot()) { //墙壁或已有机器人
            return false;
        }
        if (visited[x][y]) { //已入过队
            return false;
        }
        return true;
    }

    public Point bfs() {
        Queue<Point> q = new LinkedList<Point>();
        q.offer(from);
        while (!q.isEmpty()) {
            Point top = q.poll();
            if (top.getX() == to.getX() && top.getY() == to.getY()) {
                return top;
            }

            for (int i = 0; i < 4; i++) {//循环四次,得到四个相邻位置
                Point node = new Point();
                int newX = top.getX() + X[i];
                int newY = top.getY() + Y[i];
                if (test(newX, newY)) {//位置 (newX,newY)有效
                    node.setX(newX);
                    node.setY(newY);//设置Node的坐标是(newX,newY)
                    node.setStep(top.getStep() + 1);//Node层数为top的层数+1
                    node.getNodes().addAll(top.getNodes());
                    node.getNodes().add(node);
                    q.offer(node);//将结点Node加入队列
                    visited[newX][newY] = true;//设置位置  (newX,newY) 已经入过队
                }
            }
        }
        return null;//无法到达终点T时返回null
    }
}

点对象:

import java.util.ArrayList;
import java.util.List;

public class Point {

    private int robotId;

    private int x;

    private int y;

    private int step;//step为从起点到终点的最少步数

    private List<Point> nodes = new ArrayList<>();//所有经过的点(包括起点和终点)

    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 int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public List<Point> getNodes() {
        return nodes;
    }

    public void setNodes(List<Point> nodes) {
        this.nodes = nodes;
    }

    public int getRobotId() {
        return robotId;
    }

    public void setRobotId(int robotId) {
        this.robotId = robotId;
    }
}

地图对象

public class PlayCell {
    private boolean isObstacle;

    private boolean isRobot;

    private int energy;

    private int score;

    private int owner;

    private int warrantyPeriod;

    public PlayCell(int score) {
        this.score = score;
    }

    public boolean isObstacle() {
        return isObstacle;
    }

    public void setObstacle(boolean obstacle) {
        isObstacle = obstacle;
    }

    public int getEnergy() {
        return energy;
    }

    public void setEnergy(int energy) {
        this.energy = energy;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getOwner() {
        return owner;
    }

    public void setOwner(int owner) {
        this.owner = owner;
    }

    public int getWarrantyPeriod() {
        return warrantyPeriod;
    }

    public void setWarrantyPeriod(int warrantyPeriod) {
        this.warrantyPeriod = warrantyPeriod;
    }

    public boolean isRobot() {
        return isRobot;
    }

    public void setRobot(boolean robot) {
        isRobot = robot;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值