예제 #1
0
/**
main

The driver of a Rush Hour solver. Reports the number of moves required to solve
given rush hour puzzles.

@return programSuccess   The success/error code of the program's execution.

@pre
-# There is valid data waiting to be provided to the program.

@post
-# A number of Rush Hour puzzles will have been solved

@code
@endcode
*/
int main()
{
  // variables
  bool keepSolving = true;
  int scenarioNumber = 1;
  int numCars = 0;
  int numMoves = 0;
  GameState initialState;
  queue< GameState > gameStates;
  map< string, int > observedStates;

  // continue processing input until the signal to stop is given
  while( keepSolving )
  {
    // read in the problem size
    cin >> numCars;

    // case: there is a problem to solve
    if( numCars > 0 )
    {
      // read in the problem setup
      initialState.clear();
      initialState.setNumCars( numCars );
      initialState.readIn();

      // attempt to find a solution
      numMoves = solveRushHour( numCars, initialState, gameStates,
                                observedStates );

      // output the solution result
      // case: the problem has a solution
      if( numMoves >= 0 )
      {
        // report this
        cout << "Scenario " << scenarioNumber << " requires "
             << numMoves << " moves" << endl;
      }
      // case: the problem was not solvable
      else if( numMoves == kUnsolvable )
      {
        // report this
        cout << "Scenario: " << scenarioNumber
             << " is not solvable" << endl; 
      }

      // setup for a new problem
      scenarioNumber++;
      observedStates.clear();
      while( !gameStates.empty() )
      {
        gameStates.pop();
      }
    }
    // case: we are done solving
    else
    {
      // set signal to stop solving
      keepSolving = false;
    }
  }

  // return 0 for successful program execution
  return 0;
}