工具类:
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;
}
}