c++ - How do you find the shortest path after the BFS algorithm? -


while( !q.is_empty() ) {   loc = q.remove_from_front();    //cout << loc.row << " " << loc.col << endl;   if( (loc.row-1) >= 0) //north   {      loc2.row = loc.row-1;      loc2.col = loc.col;       if(maze[loc2.row][loc2.col] != '#' && visited[loc2.row][loc2.col] == false)      {         visited[loc2.row][loc2.col] = true;          q.add_to_back(loc2);          //loc = predecessor[loc.row][loc.col];         predecessor[loc2.row][loc2.col] = loc;          if(maze[loc2.row][loc2.col] == 'f')         {            result = 1;            maze[loc2.row][loc2.col] = '*';             break;         }      }   }    if(loc.col-1 >= 0) //west   {      loc2.row = loc.row;      loc2.col = loc.col-1;       if(maze[loc2.row][loc2.col] != '#' && visited[loc2.row][loc2.col] == false)      {         visited[loc2.row][loc2.col] = true;          q.add_to_back(loc2);          //loc = predecessor[loc.row][loc.col];         predecessor[loc2.row][loc2.col] = loc;          if(maze[loc2.row][loc2.col] == 'f')         {            result = 1;            maze[loc2.row][loc2.col] = '*';             break;         }      }   }    if(loc.row+1 < rows) //south   {      loc2.row = loc.row+1;      loc2.col = loc.col;       if(maze[loc2.row][loc2.col] != '#' && visited[loc2.row][loc2.col] == false)      {         visited[loc2.row][loc2.col] = true;          q.add_to_back(loc2);         // loc = predecessor[loc.row][loc.col];        predecessor[loc2.row][loc2.col] = loc;          if(maze[loc2.row][loc2.col] == 'f')         {            result = 1;            maze[loc2.row][loc2.col] = '*';             break;         }      }   }    if(loc.col+1 < cols) //east   {      loc2.row = loc.row;      loc2.col = loc.col+1;       if(maze[loc2.row][loc2.col] != '#' && visited[loc2.row][loc2.col] == false)      {         visited[loc2.row][loc2.col] = true;          q.add_to_back(loc2);          //loc = predecessor[loc.row][loc.col];         predecessor[loc2.row][loc2.col] = loc;          if(maze[loc2.row][loc2.col] == 'f')         {            result = 1;            maze[loc.row][loc.col] = '*';             break;         }      }   } }  if(result == 1) {    while()   {       //not sure...   } } 

this bfs algorithm , main reason asked question because other questions similar own question tends done vectors. haven't learn vectors yet. trying print shortest path using '' characters display on valid elements. should in bounds, no walls visited (walls '#' characters), , no element should visited twice. know if set predecessor 2d array correctly, shortest path should displayed correctly. however, not sure if did set correctly , how fill in path '' characters...


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

javascript - Trying create a translator based on a preset alphabet -