Source of isPath.cpp


  1: //  Created by Frank M. Carrano and Tim Henry.
  2: //  Copyright (c) 2013 __Pearson Education__. All rights reserved.

  4: /** Tests whether a sequence of flights exists between two cities.
  5:  @pre  originCity and destinationCity both exist in the flight map.
  6:  @post  Cities visited during the search are marked as visited
  7:     in the flight map.
  8:  @param originCity  The origin city.
  9:  @param destinationCity  The destination city.
 10:  @return  True if a sequence of flights exists from originCity
 11:     to destinationCity; otherwise returns false. */
 12: bool Map::isPath(City originCity, City destinationCity)
 13: {
 14:    bool result, done;
 15:    // Mark the current city as visited
 16:    
 17:    markVisited(originCity);
 18:    // Base case: the destination is reached
 19:    if (originCity == destinationCity)
 20:       result = true;
 21:    else // Try a flight to each unvisited city
 22:    {
 23:       done = false;
 24:       City nextCity = getNextCity(originCity);
 25:       while (!done && (nextCity != NO_CITY))
 26:       {
 27:          done = isPath(nextCity, destinationCity);
 28:          if (!done)
 29:             nextCity = getNextCity(originCity);
 30:       } // end while
 31:       
 32:       result = done;
 33:    }  // end if
 34:    
 35:    return result;
 36: }  // end isPath