BFS算法实现迷宫问题

BFS实现迷宫问题

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

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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);
}
}

}

文章作者: Xu Ziao
文章链接: http://www.xuziao.cn/2022/09/23/225803/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 青橙技术栈