[英]Breadth-First Search takes way too long to solve maze
我有一個像這樣的大而開放的迷宮:
############################################################
#.....................#...#................#.........#.....#
#..##.......x.........#.#.#...#.........x..#......#..#.....#
#...#.......#....#....#.#.#...#...####.....####...#..#####.#
#.....###...#....#....###.#...#.........#.........#......#.#
####.#..#...#....#........#...#####..#..#...#######..##..#.#
#....##.....#....#................#..####......#...........#
#......##...######........x....................#......######
#....##........................................#...........#
#.........##########...############........#...#...####....#
#.....#...#..#..#..#......#.......#........#...#....###....#
#######...#..#..#..#......#.......#.....####...............#
#.....#...#..#..#..#......#.......#........................#
#.....#####..#..#..#......#.......######............x......#
#.........................#................................#
#..................x......#..##..........#####.............#
#.........................####.............................#
#..........................................................#
#....##.....#....#................#..####......#...........#
#.....s##...######.............................#......######
#....##........................................#...........#
#.........##########...############........#...#...####....#
#.....#...#..#..#..#......#.......#........#...#....###....#
#######...#..#..#..#......#.......#...x.####...............#
#.....#...#..#...x.#......#.......#.................#......#
#.....#####..#..#..#...#..###....#######............######.#
#...............#..x...#..#.....##...........####...#......#
#...............#......#.........#.........##...#..........#
#...............#......#.........#..........#............#.#
############################################################
“s”是起點。 並且有多個目的地點“x”。 我只需要找到一個目的地。 如果目的地靠近起點,BFS 算法可以很快找到解決方案。 如果它們像上面的例子一樣離得更遠,則需要無窮無盡的時間。 所以我的問題是:a) 算法對這種特定類型的迷宮是否不利,我應該使用 A* 或類似的東西。 b) 我的實施不好嗎?
執行:
public class BFS {
public static String getPath(String[][] map) {
String[] ways = { "L", "R", "U", "D" }; // directions to go
Queue<String> q = new LinkedList<>();
q.offer("");
String path = "";
while (!foundBait(map, path)) {
path = q.poll();
for (String s : ways) {
String newPath = path + s;
if (valid(map, newPath))
q.offer(newPath);
}
}
return path;
}
private static boolean foundBait(String[][] map, String moves) {
int xStart = 0;
int yStart = 0;
for (int y = 0; y < map.length; y++)
for (int x = 0; x < map[0].length; x++)
if (map[y][x].equals("s")) {
xStart = x;
yStart = y;
}
int i = xStart;
int j = yStart;
for (int s = 0; s < moves.length(); s++) {
if (moves.charAt(s) == "L".charAt(0))
i--;
else if (moves.charAt(s) == "R".charAt(0))
i++;
else if (moves.charAt(s) == "U".charAt(0))
j--;
else if (moves.charAt(s) == "D".charAt(0))
j++;
}
if (map[j][i].equals("x"))
return true;
return false;
}
private static boolean valid(String[][] map, String moves) {
int xStart = 0;
int yStart = 0;
for (int y = 0; y < map.length; y++)
for (int x = 0; x < map[0].length; x++)
if (map[y][x].equals("s")) {
xStart = x;
yStart = y;
}
int i = xStart;
int j = yStart;
for (int s = 0; s < moves.length(); s++) {
if (moves.charAt(s) == "L".charAt(0))
i--;
else if (moves.charAt(s) == "R".charAt(0))
i++;
else if (moves.charAt(s) == "U".charAt(0))
j--;
else if (moves.charAt(s) == "D".charAt(0))
j++;
if (!(0 <= i && i < map[0].length && 0 <= j && j < map.length))
return false;
else if (map[j][i].equals("#") || map[j][i].equals("-"))
return false;
}
return true;
}
}
如評論中所述,問題不在於標記添加到路徑的節點,解決方案是使用第二個矩陣進行標記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.