Skip to main content
. 2020 May 20;20(10):2899. doi: 10.3390/s20102899
Algorithm 2: Get the distance to walk with BFS
Input: room, width, height, xperson, yperson
Output: Exit’s x-position (row), exit’s y-position (col), distance the person must walk - # of
       boxes (distance), An array with each of the positions (x, y) where the person walks (path)
source = QItem(yperson -1, xperson -1,0);
   visited = np.zeros((int(height),int(width)), dtype=int)
  repeat
   repeat
   if room[i][j] == b’0’ then
     visited[i][j] = 1
   else
     visited[i][j] = 0
   end
   until range(0, int(width);
until range(0, int(height);
q = queue.Queue(); q.put(source); visited[source.row][source.col] = 1
  repeat
  p = q.get()
   if room[p.row][p.col] == b’e’ then
    return p.row +1, p.col +1, dist, bfsPathExit(room, width, height, (xperson -1,yperson -1))
  end
  The function bfsPathExit is calculated with the Algorithm 2.
   if p.row - 1 >= 0 and visited[p.row - 1][p.col] == 0 then
    q.put(QItem(p.row - 1, p.col, p.dist + 1))
      visited[p.row - 1][p.col] = 1
   end
   if p.row + 1 < int(height) and visited[p.row + 1][p.col] == 0 then(
    q.put(QItem(p.row + 1, p.col, p.dist + 1))
      visited[p.row + 1][p.col] = 1
  end
  if p.col - 1 >= 0 and visited[p.row][p.col - 1] == 0 then
    q.put(QItem(p.row, p.col - 1, p.dist + 1))
      visited[p.row][p.col - 1] = 1
  end
  if p.col + 1 < int(width) and visited[p.row][p.col + 1] == 0 then
    q.put(QItem(p.row,p.col + 1, p.dist + 1))
      visited[p.row][p.col + 1] = 1
  end
until q.empty() == False;
 return -1