网格中的最短路径

时间:2016-06-13 07:49:38

标签: algorithm graph grid shortest-path breadth-first-search

我有一个像

这样的二维矩阵
A......... 
##....##..
.####...##
..........
.......###
B.........
####...### 
#...#.###.

'代表路径和'代表墙。我必须找到从 A 点到 B 的最短路径。我熟悉 BFS 算法,它看起来像一个合理的问题算法。但我觉得在网格上应用会让人感到困惑。任何人都建议在这个问题中用一些伪代码实现 BFS 算法吗?

2 个答案:

答案 0 :(得分:2)

BFS算法基本上是:

1 。将源顶点加入并标记为已访问。

2 。虽然Q不为空,重复3和4。

第3 即可。执行deque和dequed vertex x,执行

<强> 4 即可。对于x的所有相邻顶点,不访问它们并将它们标记为已访问并将它们排入Q.

这就是基本算法,如果我们一步一步地将这些步骤应用到网格中,我们唯一要注意的是网格中的单元格可能有8个邻居,我们必须检查遍历邻居之前的边界条件,以避免数组索引超出范围。

假设我们处于A(a,b)位置,我们希望达到B(c,d)。我们遵循类似的4个步骤,但进行了一些修改如下:

1 。制作一个2-d点的Q,(你可以用Java这样的语言轻松完成,通过制作一个2-d点然后Q的对象那个班级

2 。制作一个类型为boolean的网格大小的二维数组visited,初始化为false。

3 。制作一个整数类型网格大小的二维数组distance,用于距离。

让网格的大小为nxm

现在伪代码如下:

Enqueue A(a,b) to the Q

Mark dist[a][b] = 0 and visited[a][b] = true

while( Q is not empty )
{
 Point p = deque(Q)

 if( p matches B(c,d) )
 {
   print( Yes path is possible from A to B with distance[c][d] )
   return
 }
 else
 {
  //Now all we have to do is check all the possible neighbour cells according
  // to our current position in the grid
  // So we have to check all the 8 neighbour cells
   if(p.y < m - 1)
   {
    if( grid[p.x][p.y + 1] != '#' and visited[p.x][p.y + 1] == false )
    {
     enqueue (p.x,p.y + 1) to the Q // step s1
     distance[p.x][p.y + 1] = distance[p.x][p.y] + 1 // step s2
     visited[p.x][p.y + 1] = true // step s3
    }
   }
   if(p.x < n - 1)
   {
    if( grid[p.x + 1][p.y] != '#' and visited[p.x + 1][p.y] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x + 1,p.y)
    }
   }
   if(p.y > 0)
   {
    if( grid[p.x][p.y - 1] != '#' and visited[p.x][p.y - 1] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x,p.y - 1)
    }
   }
   if(p.x > 0)
   {
    if( grid[p.x - 1][p.y] != '#' and visited[p.x - 1][p.y] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x - 1,p.y)
    }
   }
   if(p.x > 0 and p.y > 0)
   {
    if( grid[p.x - 1][p.y - 1] != '#' and visited[p.x - 1][p.y - 1] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x - 1,p.y - 1)
    }
   }
   if(p.x > 0 and p.y < m-1)
   {
    if( grid[p.x - 1][p.y + 1] != '#' and visited[p.x - 1][p.y + 1] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x - 1,p.y + 1)
    }
   }
   if(p.x < n-1 and p.y > 0)
   {
    if( grid[p.x + 1][p.y - 1] != '#' and visited[p.x + 1][p.y - 1] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x + 1,p.y - 1)
    }
   }
   if(p.x < n-1 and p.y < m-1)
   {
    if( grid[p.x + 1][p.y + 1] != '#' and visited[p.x + 1][p.y + 1] == false )
    {
     Repeat steps s1,s2,s3 for point (p.x + 1,p.y + 1)
    }
   }
 }     
}
print( the path is not possible )

答案 1 :(得分:1)

非常基本上,将每个点(.)视为一个节点,并且作为邻居的每两个点也应该在它们之间具有边*。通过这种方式,您可以获得一个图表,其中包含路径可以通过的所有可能位置,并且它只包含有效边。

从那里开始运行BFS非常容易......

*如果允许您沿对角线移动,则还应添加这些边缘。这已经进入了“邻居”的定义。