Taxi

Time limit1sMemory limit1024 MB

Summary
Simulate a taxi that follows a fixed list of movement and pickup commands, tracking fuel, refueling at three priced gas stations, passenger capacity, fares, and check the end conditions.
Level

Medium6 of 10

Topics
Simulation, Implementation, Math, Greedy
Solved
No attempts yet

Problem

Taxi is a programming language in which code is expressed as a taxi moving through a fictional city and picking up passengers. The city map is shown below.

taxi

(image from https://bigzaphod.github.io/Taxi/)

A program that prints "Hello, world!" in this language looks like this.

"Hello, World!" is waiting at the Writer's Depot. Go to Writer's Depot: west 1st left, 2nd right, 1st left, 2nd left. Pickup a passenger going to the Post Office. Go to the Post Office: north 1st right, 2nd right, 1st left. Go to the Taxi Garage: north 1st right, 1st left, 1st right.

The taxi always starts at Taxi Garage, and when the program ends it must be there with no passenger on board. The taxi can hold A gallons of fuel, and starts with a full tank. At each location the taxi can pick up a passenger who wants to go to a specific destination, and this passenger gets off as soon as the taxi arrives at the destination. The passenger pays B won per mile traveled in the taxi. The taxi has a seating capacity, so it can carry at most three passengers.

(Unlike the original setting, for ease of calculation) assume the city has horizontal and vertical roads spaced 1 mile apart, and every location lies only at an intersection of two roads. Then every location can be represented by integer coordinates, and the distance between two locations is the Manhattan distance. That is, if the coordinates of two locations are (x1, y1) and (x2, y2), the distance is |x1 - x2| + |y1 - y2|. Note that even if the taxi passes a location while driving, a passenger whose destination is that location cannot get off; a passenger can get off only when the taxi arrives exactly at the destination.

The taxi can travel C miles per gallon, and it must not run out of fuel while driving. The city has three gas stations. So to keep driving, the taxi must refuel at a gas station before its fuel drops below 0. Three of the input locations are gas stations; arriving at one automatically refuels the taxi, and the three gas stations may have different prices per gallon. If the taxi has enough money to fill the tank, it pays that amount and fills the tank; otherwise it pays all the money it has and refuels. If the money needed to fill the tank is not an integer, the fractional part is truncated. That is, if filling the tank requires 1,234.567... won and the taxi currently has at least 1,234 won, it pays 1,234 won and fills the tank. If there is a passenger whose destination is the gas station, the passenger gets off first, pays the fare, and then the taxi refuels.

Now, based on this specification, write a program that decides whether the taxi completes its run while satisfying all the rules, given the city information and the taxi's movement path.

Input

The first line contains the integers A, B, C, which are the taxi's fuel capacity, the money a passenger pays per mile, and the distance the taxi can travel per gallon. (1 ≤ A ≤ 100, 1 ≤ B ≤ 100, 1 ≤ C ≤ 100)

The next line contains the number of locations N. (4 ≤ N ≤ 100)

Then N lines follow, each giving the name Di of the i-th location and its integer coordinates xi, yi. (0 ≤ xi, yi ≤ 100) The characters allowed in a name are letters, the apostrophe ('), and spaces. A name is 1 to 30 characters long, its first and last characters cannot be spaces, and it never contains two or more consecutive spaces. All location names are distinct, and a location named Taxi Garage always exists. Letters in names are case-sensitive.

Then three lines follow, each giving a gas station name Gi and its price per gallon Pi. The three gas station names are all distinct, and each is one of the locations entered above. The price per gallon is an integer between 1 and 100. Taxi Garage cannot be a gas station.

Next, the number of statements K is given. (1 ≤ K ≤ 10,000)

Then K lines follow, each containing a statement in one of the following two forms.

  • Go to (X).: move to X. X is one of the entered locations.
  • Pickup a passenger going to (X).: pick up a passenger going to X. X is one of the entered locations, and neither the location where the taxi currently is nor Taxi Garage can be the destination.

Output

If the run is completed according to the rules, print the amount of money finally earned.

If the run fails, print one of the following strings.

  • OUT OF GAS: when the taxi runs out of fuel while driving
  • CAPACITY FULL: when trying to pick up more passengers than the capacity
  • NOT IN GARAGE: when the last location is not Taxi Garage
  • REMAINING PASSENGER: when the last location is Taxi Garage but a passenger has not yet gotten off

Examples5

  1. Example 1

    Input
    50 20 10
    5
    Taxi Garage 50 50
    Zoom Zoom 13 66
    Fueler Up 39 27
    What's The Difference 95 32
    Go More 2 34
    Fueler Up 55
    Go More 33
    Zoom Zoom 17
    7
    Go to Go More.
    Pickup a passenger going to Fueler Up.
    Go to Zoom Zoom.
    Go to Fueler Up.
    Go to What's The Difference.
    Go to Go More.
    Go to Taxi Garage.
    
    Expected output
    700
    
  2. Example 2

    Input
    10 20 10
    4
    Zoom Zoom 30 40
    Fueler Up 100 0
    Taxi Garage 0 0
    Go More 0 100
    Go More 40
    Zoom Zoom 50
    Fueler Up 60
    3
    Go to Go More.
    Go to Fueler Up.
    Go to Taxi Garage.
    
    Expected output
    OUT OF GAS
    
  3. Example 3

    Input
    30 40 50
    5
    Station A 0 0
    Station B 10 10
    Taxi Garage 20 20
    Station C 30 30
    KonKat's 40 40
    Station A 10
    Station B 10
    Station C 10
    8
    Go to Station A.
    Pickup a passenger going to KonKat's.
    Go to Station B.
    Pickup a passenger going to KonKat's.
    Pickup a passenger going to Station A.
    Go to Station C.
    Pickup a passenger going to KonKat's.
    Go to Taxi Garage.
    
    Expected output
    CAPACITY FULL
    
  4. Example 4

    Input
    30 30 30
    4
    Taxi Garage 1 2
    A 3 4
    B 5 6
    C 7 8
    A 10
    B 20
    C 30
    1
    Go to A.
    
    Expected output
    NOT IN GARAGE
    
  5. Example 5

    Input
    30 30 30
    4
    Taxi Garage 1 2
    A 3 4
    B 5 6
    C 7 8
    A 10
    B 20
    C 30
    3
    Go to A.
    Pickup a passenger going to B.
    Go to Taxi Garage.
    
    Expected output
    REMAINING PASSENGER