samples/13_path_finding_algorithms/04_early_exit/app/main.rb
# Moves the search forward one step
Moves the search forward one step
Moves the search forward one step
def step
    if state.visited.empty?
      state.visited[state.star] = true
      state.early_exit_visited[state.star] = true
      state.frontier << state.star
      state.came_from[state.star] = nil
    end
    unless state.frontier.empty?
      new_frontier = state.frontier.shift
      adjacent_neighbors(new_frontier).each do |neighbor|
        unless state.visited.has_key?(neighbor) || state.walls.has_key?(neighbor)
          state.visited[neighbor] = true
          unless state.visited.has_key?(state.target)
            state.early_exit_visited[neighbor] = true
          end
          state.frontier << neighbor
          state.came_from[neighbor] = new_frontier
        end
      end
    end
  end
def step if state . visited . empty? state . visited [ state . star ] = true state . early_exit_visited [ state . star ] = true state . frontier << state . star state . came_from [ state . star ] = nil end unless state . frontier . empty? new_frontier = state . frontier . shift adjacent_neighbors ( new_frontier ) . each do | neighbor | unless state . visited . has_key? ( neighbor ) || state . walls . has_key? ( neighbor ) state . visited [ neighbor ] = true unless state . visited . has_key? ( state . target ) state . early_exit_visited [ neighbor ] = true end state . frontier << neighbor state . came_from [ neighbor ] = new_frontier end end end end
Moves the search forward one step
Moves the search forward one step
# The setup to the search # Runs once when there are no visited cells # A step in the search # Takes the next frontier cell # For each of its neighbors # That have not been visited and are not walls # Add them to the frontier and mark them as visited in the first grid # Unless the target has been visited # Mark the neighbor as visited in the second grid as well # Add the neighbor to the frontier and remember which cell it came from