[英]Simple Breadth First Search not finding target
這是一個標准的 BFS,尋找通過 1 和 0 的二維數組迷宮找到 9 的路徑。
出隊時,節點坐標作為字符串存儲到 hashMap。只有在邊界內、包含 0 或 9 且不在 hashMap 鍵中的項目才會添加到隊列中。
循環在沒有到達目標的情況下退出。 我不知道哪一部分是錯的。
public static Queue<Box> q = new LinkedList<Box>();
public static HashMap<String,Boolean> map_seen = new HashMap<String,Boolean>();
public static void searchPath(int[][] maze, int x, int y, ArrayList<Integer> path) {
q.add(new Box(x,y,null));
while(!q.isEmpty()) {
Box p = q.poll();
String ps = Integer.toString(p.x) + Integer.toString(p.y);
map_seen.put(ps, true);
if (maze[p.y][p.x] == 9) {
System.out.println("target found! ");
getPath(p, maze, path);
return;
}
if(isFree(maze, p.x+1,p.y)) {
Box nextP= new Box(p.x+1,p.y,p);
String nextS= Integer.toString(p.x+1) + Integer.toString(p.y);
if(!map_seen.containsKey(nextS)) {
q.add(nextP);
}
}
if(isFree(maze, p.x-1,p.y)) {
Box nextP= new Box(p.x-1,p.y,p);
String nextS= Integer.toString(p.x-1) + Integer.toString(p.y);
if(!map_seen.containsKey(nextS)) {
q.add(nextP);
}
}
if(isFree(maze, p.x,p.y+1)) {
Box nextP= new Box(p.x,p.y+1,p);
String nextS= Integer.toString(p.x) + Integer.toString(p.y+1);
if(!map_seen.containsKey(nextS)) {
q.add(nextP);
}
}
if(isFree(maze, p.x,p.y-1)) {
Box nextP= new Box(p.x,p.y-1,p);
String nextS= Integer.toString(p.x) + Integer.toString(p.y-1);
if(!map_seen.containsKey(nextS)) {
q.add(nextP);
}
}
}
System.out.println("exited reached");
}
public static boolean isFree(int[][] maze, int x, int y) {
if((x >= 0 && x < maze[0].length) && (y >= 0 && y < maze.length) && (maze[y][x] == 0 || maze[y][x] == 9)) {
return true;
}
return false;
}
public static ArrayList<Integer> getPath(Box node, int[][] maze, ArrayList<Integer> path){
while(node!=null){
path.add(node.x);
path.add(node.y);
maze[node.y][node.x] = 2;
node = node.parent;
}
return path;
}
我認為您的代碼可能會像這樣更改。
public static Queue<Box> q = new LinkedList<Box>();
public static HashMap<String,Boolean> map_seen = new HashMap<String,Boolean>();
public static void searchPath(int[][] maze, int x, int y, ArrayList<Integer> path) {
auto b = new Box(x,y,null);
q.add(b);
map_seen.put(*b, true);
while(!q.isEmpty()) {
Box p = q.poll();
String ps = Integer.toString(p.x) + Integer.toString(p.y);
if (maze[p.y][p.x] == 9) {
System.out.println("target found! ");
getPath(p, maze, path);
return;
}
if(isFree(maze, p.x+1,p.y)) {
Box nextP= new Box(p.x+1,p.y,p);
String nextS= Integer.toString(p.x+1) + Integer.toString(p.y);
if(!map_seen.containsKey(nextS)) {
map_seen.put(nextS, true);
q.add(nextP);
}
}
if(isFree(maze, p.x-1,p.y)) {
Box nextP= new Box(p.x-1,p.y,p);
String nextS= Integer.toString(p.x-1) + Integer.toString(p.y);
if(!map_seen.containsKey(nextS)) {
map_seen.put(nextS, true);
q.add(nextP);
}
}
if(isFree(maze, p.x,p.y+1)) {
Box nextP= new Box(p.x,p.y+1,p);
String nextS= Integer.toString(p.x) + Integer.toString(p.y+1);
if(!map_seen.containsKey(nextS)) {
map_seen.put(nextS, true);
q.add(nextP);
}
}
if(isFree(maze, p.x,p.y-1)) {
Box nextP= new Box(p.x,p.y-1,p);
String nextS= Integer.toString(p.x) + Integer.toString(p.y-1);
if(!map_seen.containsKey(nextS)) {
map_seen.put(nextS, true);
q.add(nextP);
}
}
}
System.out.println("exited reached");
}
}
我不知道這是否是唯一的問題,但使用
String ps = Integer.toString(p.x) + Integer.toString(p.y);
作為鑰匙肯定是錯誤的。 框[1, 10]
和[11, 0]
產生相同的字符串。 這意味着一旦您訪問[1, 10]
,您將認為[11, 0]
已訪問,並且永遠不會探索其路徑。 至少加個分隔符
String ps = Integer.toString(p.x) + ":" + Integer.toString(p.y);
但為什么不使用盒子本身作為鑰匙呢?
作為旁注, map<whatever, bool>
是一個類固醇集合。 就用一套吧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.