Foundations/A_Star_Search/solution.cpp
vector<vector<State>> Search(vector<vector<State>> grid, int init[2], int goal[2]) {
  // Create the vector of open nodes.
  vector<vector<int>> open {};
  
  // Initialize the starting node.
  int x = init[0];
  int y = init[1];
  int g = 0;
  int h = Heuristic(x, y, goal[0],goal[1]);
  AddToOpen(x, y, g, h, open, grid);

  while (open.size() > 0) {
    // Get the next node
    CellSort(&open);
    auto current = open.back();
    open.pop_back();
    x = current[0];
    y = current[1];
    grid[x][y] = State::kPath;

    // Check if we're done.
    if (x == goal[0] && y == goal[1]) {
      grid[init[0]][init[1]] = State::kStart;
      grid[goal[0]][goal[1]] = State::kFinish;
      return grid;
    }
    
    // If we're not done, expand search to current node's neighbors.
    ExpandNeighbors(current, goal, open, grid);
  }
  
  // We've run out of new nodes to explore and haven't found a path.
  cout << "No path found!" << "\n";
  return std::vector<vector<State>>{};
}
/** 
 * Implementation of A* search algorithm
 */
Implementation of A* search algorithm
Implementation of A * search algorithm
vector<vector<State>> Search(vector<vector<State>> grid, int init[2], int goal[2]) {
  vector<vector<int>> open {};
  int x = init[0];
  int y = init[1];
  int g = 0;
  int h = Heuristic(x, y, goal[0],goal[1]);
  AddToOpen(x, y, g, h, open, grid);
  while (open.size() > 0) {
    CellSort(&open);
    auto current = open.back();
    open.pop_back();
    x = current[0];
    y = current[1];
    grid[x][y] = State::kPath;
    if (x == goal[0] && y == goal[1]) {
      grid[init[0]][init[1]] = State::kStart;
      grid[goal[0]][goal[1]] = State::kFinish;
      return grid;
    }
    ExpandNeighbors(current, goal, open, grid);
  }
  cout << "No path found!" << "\n";
  return std::vector<vector<State>>{};
}
vector < vector < State > > Search ( vector < vector < State > > grid , int init [ 2 ] , int goal [ 2 ] ) { vector < vector < int > > open { } ; int x = init [ 0 ] ; int y = init [ 1 ] ; int g = 0 ; int h = Heuristic ( x , y , goal [ 0 ] , goal [ 1 ] ) ; AddToOpen ( x , y , g , h , open , grid ) ; while ( open . size ( ) > 0 ) { CellSort ( & open ) ; auto current = open . back ( ) ; open . pop_back ( ) ; x = current [ 0 ] ; y = current [ 1 ] ; grid [ x ] [ y ] = State :: kPath ; if ( x == goal [ 0 ] && y == goal [ 1 ] ) { grid [ init [ 0 ] ] [ init [ 1 ] ] = State :: kStart ; grid [ goal [ 0 ] ] [ goal [ 1 ] ] = State :: kFinish ; return grid ; } ExpandNeighbors ( current , goal , open , grid ) ; } cout << " " << " \n " ; return std :: vector < vector < State > > { } ; }
Implementation of A* search algorithm
Implementation of A * search algorithm
// Create the vector of open nodes. // Initialize the starting node. // Get the next node // Check if we're done. // If we're not done, expand search to current node's neighbors. // We've run out of new nodes to explore and haven't found a path.