广度优先搜索不返回最短路径

时间:2015-01-04 17:40:28

标签: java depth-first-search a-star breadth-first-search

我正在尝试使用Java的广度优先搜索算法。考虑到10x10网格,我试图找到最后一个单元9x9(网格从0,0开始)。到达9x9时,它已遍历网格中的所有单元格。我听说BFS会给我最短的路径。但实际上它给了我最长的路径。

  1. 你能否告诉我这是否是预期的行为?
  2. 如果这是BFS的工作方式,那么获得9x9单元最短路线的最佳方法是什么?
  3. 请建议。

    编辑 - 我已经使用了这个逻辑并完成了我的游戏。如果您想参考,请查看https://play.google.com/store/apps/details?id=com.game.puzzle.game.ballmania.android

    代码

    package com.example.game.bfs.alogrithm;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Queue;
    
    public class BFS {
    
    static class Cell {
        private int x;
        private int y;
        private String value;
        private boolean visitStatus;
    
        public Cell(int x, int y, String value,boolean visitStatus) {
            this.x = x;
            this.y = y;
            this.value = value; 
            this.visitStatus=visitStatus;
        }
    }
    
    private Cell[][] board;
    
    private List<Cell> visited = new ArrayList<Cell>();
    
    private boolean testDone;
    
    public void setBoard(Cell[][] board) {
        this.board = board;
    } 
    
    public Cell getAdjacentUnvisitedCell(Cell cell)
    {  
       int moves[][] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
       for (int n = 0; n < 4 /* moves.length */; ++n) {
           int ti = cell.x + moves[n][0];
           int tj = cell.y + moves[n][1];
          // System.out.println("ti,tj" + ti +"," + tj );  
    
           if (ti >= 0 && ti < board.length && tj >= 0 && tj < board[0].length) { 
    
              // System.out.println("getAdjacentUnvisitedCell : " + "[" + board[ti][tj].x + "," + board[ti][tj].y + "]" ); 
              // System.out.println("getAdjacentUnvisitedCell : board[ti][tj].visitStatus " + board[ti][tj].visitStatus ); 
    
               if(!board[ti][tj].visitStatus) {  
                  return board[ti][tj];
               }
           }
       }  
       return null;  
    } 
    
    public void BFSearch(Cell start, Cell end) {  
       // BFS uses Queue data structure 
       Queue<Cell> q = new LinkedList<Cell>(); 
       q.add(start);
       visited.add(start);
       board[start.x][start.y].visitStatus = true;
    
       //printNode(start);
    
       while( !q.isEmpty() )
       { 
          Cell c; 
          c = q.peek(); 
          Cell unVisitedadjCell = getAdjacentUnvisitedCell(c); 
    
          if(!testDone){
              testDone=true;  
          } 
    
          if ( unVisitedadjCell != null )
          {  visited.add(unVisitedadjCell); 
             board[unVisitedadjCell.x][unVisitedadjCell.y].visitStatus = true;
    
             printNode(unVisitedadjCell,c); 
             q.add(unVisitedadjCell);
          }
          else
          {
             q.remove();
          }
       }
    
       visited.clear();     //Clear visited property of nodes
    }
    
    
    private void printNode(Cell c,Cell node) {
        System.out.println("For Node " + node.x +"," + node.y + ",   " + "Just Visited : " + "[" + c.x + "," + c.y + "]" );  
    } 
    
    public static void main(String[] args) {
        Cell[][] cells = new Cell[10][10];
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                cells[i][j] = new Cell(i, j, "defaultvalue",false);
            }
        } 
    
        BFS board = new BFS();
        board.setBoard(cells);
    
        board.BFSearch(cells[0][0], cells[1][4]);
    }
    
    
    }
    
    }
    

    记录:

    For Node 0,0,   Just Visited : [1,0]
    For Node 0,0,   Just Visited : [0,1]
    For Node 1,0,   Just Visited : [2,0]
    For Node 1,0,   Just Visited : [1,1]
    For Node 0,1,   Just Visited : [0,2]
    For Node 2,0,   Just Visited : [3,0]
    For Node 2,0,   Just Visited : [2,1]
    For Node 1,1,   Just Visited : [1,2]
    For Node 0,2,   Just Visited : [0,3]
    For Node 3,0,   Just Visited : [4,0]
    For Node 3,0,   Just Visited : [3,1]
    For Node 2,1,   Just Visited : [2,2]
    For Node 1,2,   Just Visited : [1,3]
    For Node 0,3,   Just Visited : [0,4]
    For Node 4,0,   Just Visited : [5,0]
    For Node 4,0,   Just Visited : [4,1]
    For Node 3,1,   Just Visited : [3,2]
    For Node 2,2,   Just Visited : [2,3]
    For Node 1,3,   Just Visited : [1,4]
    For Node 0,4,   Just Visited : [0,5]
    For Node 5,0,   Just Visited : [6,0]
    For Node 5,0,   Just Visited : [5,1]
    For Node 4,1,   Just Visited : [4,2]
    For Node 3,2,   Just Visited : [3,3]
    For Node 2,3,   Just Visited : [2,4]
    For Node 1,4,   Just Visited : [1,5]
    For Node 0,5,   Just Visited : [0,6]
    For Node 6,0,   Just Visited : [7,0]
    For Node 6,0,   Just Visited : [6,1]
    For Node 5,1,   Just Visited : [5,2]
    For Node 4,2,   Just Visited : [4,3]
    For Node 3,3,   Just Visited : [3,4]
    For Node 2,4,   Just Visited : [2,5]
    For Node 1,5,   Just Visited : [1,6]
    For Node 0,6,   Just Visited : [0,7]
    For Node 7,0,   Just Visited : [8,0]
    For Node 7,0,   Just Visited : [7,1]
    For Node 6,1,   Just Visited : [6,2]
    For Node 5,2,   Just Visited : [5,3]
    For Node 4,3,   Just Visited : [4,4]
    For Node 3,4,   Just Visited : [3,5]
    For Node 2,5,   Just Visited : [2,6]
    For Node 1,6,   Just Visited : [1,7]
    For Node 0,7,   Just Visited : [0,8]
    For Node 8,0,   Just Visited : [9,0]
    For Node 8,0,   Just Visited : [8,1]
    For Node 7,1,   Just Visited : [7,2]
    For Node 6,2,   Just Visited : [6,3]
    For Node 5,3,   Just Visited : [5,4]
    For Node 4,4,   Just Visited : [4,5]
    For Node 3,5,   Just Visited : [3,6]
    For Node 2,6,   Just Visited : [2,7]
    For Node 1,7,   Just Visited : [1,8]
    For Node 0,8,   Just Visited : [0,9]
    For Node 9,0,   Just Visited : [9,1]
    For Node 8,1,   Just Visited : [8,2]
    For Node 7,2,   Just Visited : [7,3]
    For Node 6,3,   Just Visited : [6,4]
    For Node 5,4,   Just Visited : [5,5]
    For Node 4,5,   Just Visited : [4,6]
    For Node 3,6,   Just Visited : [3,7]
    For Node 2,7,   Just Visited : [2,8]
    For Node 1,8,   Just Visited : [1,9]
    For Node 9,1,   Just Visited : [9,2]
    For Node 8,2,   Just Visited : [8,3]
    For Node 7,3,   Just Visited : [7,4]
    For Node 6,4,   Just Visited : [6,5]
    For Node 5,5,   Just Visited : [5,6]
    For Node 4,6,   Just Visited : [4,7]
    For Node 3,7,   Just Visited : [3,8]
    For Node 2,8,   Just Visited : [2,9]
    For Node 9,2,   Just Visited : [9,3]
    For Node 8,3,   Just Visited : [8,4]
    For Node 7,4,   Just Visited : [7,5]
    For Node 6,5,   Just Visited : [6,6]
    For Node 5,6,   Just Visited : [5,7]
    For Node 4,7,   Just Visited : [4,8]
    For Node 3,8,   Just Visited : [3,9]
    For Node 9,3,   Just Visited : [9,4]
    For Node 8,4,   Just Visited : [8,5]
    For Node 7,5,   Just Visited : [7,6]
    For Node 6,6,   Just Visited : [6,7]
    For Node 5,7,   Just Visited : [5,8]
    For Node 4,8,   Just Visited : [4,9]
    For Node 9,4,   Just Visited : [9,5]
    For Node 8,5,   Just Visited : [8,6]
    For Node 7,6,   Just Visited : [7,7]
    For Node 6,7,   Just Visited : [6,8]
    For Node 5,8,   Just Visited : [5,9]
    For Node 9,5,   Just Visited : [9,6]
    For Node 8,6,   Just Visited : [8,7]
    For Node 7,7,   Just Visited : [7,8]
    For Node 6,8,   Just Visited : [6,9]
    For Node 9,6,   Just Visited : [9,7]
    For Node 8,7,   Just Visited : [8,8]
    For Node 7,8,   Just Visited : [7,9]
    For Node 9,7,   Just Visited : [9,8]
    For Node 8,8,   Just Visited : [8,9]
    For Node 9,8,   Just Visited : [9,9]
    

    访问单元格的模式。

    enter image description here

2 个答案:

答案 0 :(得分:3)

从头到尾追溯日志。你会发现它实际上找到了最短的路径 - 沿着网格的边缘。不幸的是,在网格中,如果你不允许通过对角线(在这种情况下BFS离开窗口,因为对角线应该有不同的权重),所有路径只有操作&#34;到右边&#34; 34; &#34; down&#34;是最短的。

你可以通过简单的逻辑看到它 - 从0到9你必须做9次移动。您有2个坐标,从(0, 0)(9, 9),您只能在1个操作中更改一个坐标,因此最短路径有9+9=18步。追溯并看到此路径有18个步骤。类似地,任何路径从开始到结束只有操作to the rightdown将有18个步骤,因此任何类似的路径都是最短的。决定路径本身的只是将相邻坐标放入队列的顺序。尝试以随机顺序进行。

编辑:这里是如何计算最短路径的数量。 我们以前注意到有18个操作;其中9个是to the right,9个是down。这些操作的顺序并不重要,因为最终您已将(9, 9)添加到初始(0, 0),因此您实际到达结尾。我们如何算他们?让我们为每个操作分配一个标识符:a_1, a_2, ... a_18。我们现在要选择其中9个操作为down。因此,我们选择down操作的第一个位置,我们可以通过18种方式进行操作(因为有18种操作可供选择),然后是第二种(17方式),依此类推,直到我们&#39 ;退出down行动。我们本可以18*17*...*10方式完成这项工作。现在我们为right操作选择点。我们可以9*8*...*1方式(通过神学)这样做。但是现在我们并没有真正区分每个down指令,对吗?我们本可以down方式选择第一个9操作,8方式选择第二个right,依此类推。同样,我们可以选择(18*17*...*1)/((9*8*...*1)*(9*8*...*1)) = 48 620次操作。最后,我们推断出有9种方式(我们将操作区分为无意义)。它也是您可以从18点中选择Introductory combinatorics的方式的数量。

如果我的解释过于混乱,我建议您Richard A. Brualdi查看{{1}}。关于某些离散数学领域的有趣事情,这本书非常酷。它很容易阅读。

答案 1 :(得分:1)

根据我对其他答案的理解(这是lared),你的代码可能是这样的:

package com.example.game.bfs.alogrithm;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


/**
   http://stackoverflow.com/questions/27768394/breadth-first-search-is-not-returning-the-shortest-path
 **/
public class BFS {

static class Cell {
    private int x;
    private int y;
    private String value;
    private boolean visitStatus;
    private Cell previousCell;

    public Cell(int x, int y, String value,boolean visitStatus) {
        this.x = x;
        this.y = y;
        this.value = value; 
        this.visitStatus=visitStatus;
    }

    public String toString()
    {
    return  "[" +   x + "," +   y + "]";
    }
}

private Cell[][] board;

private List<Cell> visited = new ArrayList<Cell>();

private boolean testDone;

public void setBoard(Cell[][] board) {
    this.board = board;
} 

public Cell getAdjacentUnvisitedCell(Cell cell)
{  
    int moves[][] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
    // for diagonal moves :
    // int moves[][] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 }, {1,1} };
   for (int n = 0; n < moves.length ; ++n) {
       int ti = cell.x + moves[n][0];
       int tj = cell.y + moves[n][1];
      // System.out.println("ti,tj" + ti +"," + tj );  

       if (ti >= 0 && ti < board.length && tj >= 0 && tj < board[0].length) { 

          // System.out.println("getAdjacentUnvisitedCell : " + "[" + board[ti][tj].x + "," + board[ti][tj].y + "]" ); 
          // System.out.println("getAdjacentUnvisitedCell : board[ti][tj].visitStatus " + board[ti][tj].visitStatus ); 

           if(!board[ti][tj].visitStatus) {  
              return board[ti][tj];
           }
       }
   }  
   return null;  
} 

public void BFSearch(Cell start, Cell end) {  
   // BFS uses Queue data structure 
   Queue<Cell> q = new LinkedList<Cell>(); 
   Cell unVisitedadjCell = start; 
   Cell c =  null;
   q.add(start);
   visited.add(start);
   board[start.x][start.y].visitStatus = true;

   //printNode(start);

   while( !q.isEmpty() )
   { 
      c = q.peek(); 
      unVisitedadjCell = getAdjacentUnvisitedCell(c); 

      if(!testDone){
          testDone=true;  
      } 

      if ( unVisitedadjCell != null )
      {  visited.add(unVisitedadjCell); 
         board[unVisitedadjCell.x][unVisitedadjCell.y].visitStatus = true;

         printNode(unVisitedadjCell,c);
         unVisitedadjCell.previousCell=c; 
         q.add(unVisitedadjCell);
      }
      else
      {
         q.remove();
      }
   }
   System.out.println("Shortest path");

   while ( ( c != null ) && ( c!=start))
       {
       printNode(c.previousCell,c);
       c = c.previousCell;
       }

   visited.clear();     //Clear visited property of nodes
}


private void printNode(Cell c,Cell node) {
    System.out.println("For Node " + node.x +"," + node.y + ",  " + "Just Visited : " + c + " previous was " + node.previousCell);
} 

public static void main(String[] args) {
    Cell[][] cells = new Cell[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            cells[i][j] = new Cell(i, j, "defaultvalue",false);
        }
    }
    cells[0][1].value = "B";
    cells[0][2].value = "B";
    cells[1][1].value = "B";
    cells[1][3].value = "B";
    cells[2][1].value = "B";
    cells[2][2].value = "B";
    cells[2][3].value = "B";
    cells[2][7].value = "B";
    cells[2][8].value = "B"; 

    BFS board = new BFS();
    board.setBoard(cells);

    board.BFSearch(cells[0][0], cells[1][4]);
}


}

java -cp。 com.example.game.bfs.alogrithm.BFS

For Node 0,0,  Just Visited : [1,0] previous was null
For Node 0,0,  Just Visited : [0,1] previous was null
For Node 1,0,  Just Visited : [2,0] previous was [0,0]
For Node 1,0,  Just Visited : [1,1] previous was [0,0]
For Node 0,1,  Just Visited : [0,2] previous was [0,0]
For Node 2,0,  Just Visited : [3,0] previous was [1,0]
For Node 2,0,  Just Visited : [2,1] previous was [1,0]
For Node 1,1,  Just Visited : [1,2] previous was [1,0]
For Node 0,2,  Just Visited : [0,3] previous was [0,1]
For Node 3,0,  Just Visited : [4,0] previous was [2,0]
For Node 3,0,  Just Visited : [3,1] previous was [2,0]
For Node 2,1,  Just Visited : [2,2] previous was [2,0]
For Node 1,2,  Just Visited : [1,3] previous was [1,1]
For Node 0,3,  Just Visited : [0,4] previous was [0,2]
For Node 4,0,  Just Visited : [5,0] previous was [3,0]
For Node 4,0,  Just Visited : [4,1] previous was [3,0]
For Node 3,1,  Just Visited : [3,2] previous was [3,0]
For Node 2,2,  Just Visited : [2,3] previous was [2,1]
For Node 1,3,  Just Visited : [1,4] previous was [1,2]
For Node 0,4,  Just Visited : [0,5] previous was [0,3]
For Node 5,0,  Just Visited : [6,0] previous was [4,0]
For Node 5,0,  Just Visited : [5,1] previous was [4,0]
For Node 4,1,  Just Visited : [4,2] previous was [4,0]
For Node 3,2,  Just Visited : [3,3] previous was [3,1]
For Node 2,3,  Just Visited : [2,4] previous was [2,2]
For Node 1,4,  Just Visited : [1,5] previous was [1,3]
For Node 0,5,  Just Visited : [0,6] previous was [0,4]
For Node 6,0,  Just Visited : [7,0] previous was [5,0]
For Node 6,0,  Just Visited : [6,1] previous was [5,0]
For Node 5,1,  Just Visited : [5,2] previous was [5,0]
For Node 4,2,  Just Visited : [4,3] previous was [4,1]
For Node 3,3,  Just Visited : [3,4] previous was [3,2]
For Node 2,4,  Just Visited : [2,5] previous was [2,3]
For Node 1,5,  Just Visited : [1,6] previous was [1,4]
For Node 0,6,  Just Visited : [0,7] previous was [0,5]
For Node 7,0,  Just Visited : [8,0] previous was [6,0]
For Node 7,0,  Just Visited : [7,1] previous was [6,0]
For Node 6,1,  Just Visited : [6,2] previous was [6,0]
For Node 5,2,  Just Visited : [5,3] previous was [5,1]
For Node 4,3,  Just Visited : [4,4] previous was [4,2]
For Node 3,4,  Just Visited : [3,5] previous was [3,3]
For Node 2,5,  Just Visited : [2,6] previous was [2,4]
For Node 1,6,  Just Visited : [1,7] previous was [1,5]
For Node 0,7,  Just Visited : [0,8] previous was [0,6]
For Node 8,0,  Just Visited : [9,0] previous was [7,0]
For Node 8,0,  Just Visited : [8,1] previous was [7,0]
For Node 7,1,  Just Visited : [7,2] previous was [7,0]
For Node 6,2,  Just Visited : [6,3] previous was [6,1]
For Node 5,3,  Just Visited : [5,4] previous was [5,2]
For Node 4,4,  Just Visited : [4,5] previous was [4,3]
For Node 3,5,  Just Visited : [3,6] previous was [3,4]
For Node 2,6,  Just Visited : [2,7] previous was [2,5]
For Node 1,7,  Just Visited : [1,8] previous was [1,6]
For Node 0,8,  Just Visited : [0,9] previous was [0,7]
For Node 9,0,  Just Visited : [9,1] previous was [8,0]
For Node 8,1,  Just Visited : [8,2] previous was [8,0]
For Node 7,2,  Just Visited : [7,3] previous was [7,1]
For Node 6,3,  Just Visited : [6,4] previous was [6,2]
For Node 5,4,  Just Visited : [5,5] previous was [5,3]
For Node 4,5,  Just Visited : [4,6] previous was [4,4]
For Node 3,6,  Just Visited : [3,7] previous was [3,5]
For Node 2,7,  Just Visited : [2,8] previous was [2,6]
For Node 1,8,  Just Visited : [1,9] previous was [1,7]
For Node 9,1,  Just Visited : [9,2] previous was [9,0]
For Node 8,2,  Just Visited : [8,3] previous was [8,1]
For Node 7,3,  Just Visited : [7,4] previous was [7,2]
For Node 6,4,  Just Visited : [6,5] previous was [6,3]
For Node 5,5,  Just Visited : [5,6] previous was [5,4]
For Node 4,6,  Just Visited : [4,7] previous was [4,5]
For Node 3,7,  Just Visited : [3,8] previous was [3,6]
For Node 2,8,  Just Visited : [2,9] previous was [2,7]
For Node 9,2,  Just Visited : [9,3] previous was [9,1]
For Node 8,3,  Just Visited : [8,4] previous was [8,2]
For Node 7,4,  Just Visited : [7,5] previous was [7,3]
For Node 6,5,  Just Visited : [6,6] previous was [6,4]
For Node 5,6,  Just Visited : [5,7] previous was [5,5]
For Node 4,7,  Just Visited : [4,8] previous was [4,6]
For Node 3,8,  Just Visited : [3,9] previous was [3,7]
For Node 9,3,  Just Visited : [9,4] previous was [9,2]
For Node 8,4,  Just Visited : [8,5] previous was [8,3]
For Node 7,5,  Just Visited : [7,6] previous was [7,4]
For Node 6,6,  Just Visited : [6,7] previous was [6,5]
For Node 5,7,  Just Visited : [5,8] previous was [5,6]
For Node 4,8,  Just Visited : [4,9] previous was [4,7]
For Node 9,4,  Just Visited : [9,5] previous was [9,3]
For Node 8,5,  Just Visited : [8,6] previous was [8,4]
For Node 7,6,  Just Visited : [7,7] previous was [7,5]
For Node 6,7,  Just Visited : [6,8] previous was [6,6]
For Node 5,8,  Just Visited : [5,9] previous was [5,7]
For Node 9,5,  Just Visited : [9,6] previous was [9,4]
For Node 8,6,  Just Visited : [8,7] previous was [8,5]
For Node 7,7,  Just Visited : [7,8] previous was [7,6]
For Node 6,8,  Just Visited : [6,9] previous was [6,7]
For Node 9,6,  Just Visited : [9,7] previous was [9,5]
For Node 8,7,  Just Visited : [8,8] previous was [8,6]
For Node 7,8,  Just Visited : [7,9] previous was [7,7]
For Node 9,7,  Just Visited : [9,8] previous was [9,6]
For Node 8,8,  Just Visited : [8,9] previous was [8,7]
For Node 9,8,  Just Visited : [9,9] previous was [9,7]
Shortest path
For Node 9,9,  Just Visited : [9,8] previous was [9,8]
For Node 9,8,  Just Visited : [9,7] previous was [9,7]
For Node 9,7,  Just Visited : [9,6] previous was [9,6]
For Node 9,6,  Just Visited : [9,5] previous was [9,5]
For Node 9,5,  Just Visited : [9,4] previous was [9,4]
For Node 9,4,  Just Visited : [9,3] previous was [9,3]
For Node 9,3,  Just Visited : [9,2] previous was [9,2]
For Node 9,2,  Just Visited : [9,1] previous was [9,1]
For Node 9,1,  Just Visited : [9,0] previous was [9,0]
For Node 9,0,  Just Visited : [8,0] previous was [8,0]
For Node 8,0,  Just Visited : [7,0] previous was [7,0]
For Node 7,0,  Just Visited : [6,0] previous was [6,0]
For Node 6,0,  Just Visited : [5,0] previous was [5,0]
For Node 5,0,  Just Visited : [4,0] previous was [4,0]
For Node 4,0,  Just Visited : [3,0] previous was [3,0]
For Node 3,0,  Just Visited : [2,0] previous was [2,0]
For Node 2,0,  Just Visited : [1,0] previous was [1,0]
For Node 1,0,  Just Visited : [0,0] previous was [0,0]

然后尝试使用带有对角线的注释代码......