#P7182. Parking Lot Escape

    ID: 20386 Type: Default 1000ms 256MiB

Parking Lot Escape

Parking Lot Escape

The parking lot for BOI2004 is a 6×6 grid. Rows are numbered from 1 to 6 top‐to‐bottom and columns from 1 to 6 left‐to‐right. There is only one exit in the parking lot located at row 3, column 6.

There are n cars parked on the lot. Your car (car number 1) is a 2×1 car and is among these n vehicles. Unfortunately, your car cannot exit directly because it is blocked by other vehicles. You and your friends are allowed to move any car. However, no car (including yours) can turn or change its orientation.

There are only two types of cars:

  • A 2×1 car
  • A 3×1 car
Each car can only move along the axis corresponding to its longer side (i.e. horizontally if it is placed horizontally and vertically if placed vertically).

The input provides the starting positions and orientations of the n cars. For each car, the starting position is given by the row and column of its upper‐left (or leftmost in case of a horizontal car, and topmost for vertical car) cell. For a horizontally placed car of length L, the allowed positions are those with 1 ≤ column ≤ 6 − L + 1 (except for car 1 which may move to exit). For a vertically placed car of length L, the allowed starting rows are 1 ≤ row ≤ 6 − L + 1.

Your car (car 1) is guaranteed to be a 2×1 horizontal car placed on row 3. To exit the parking lot, car 1 must move so that its rightmost cell goes beyond column 6. In other words, if the left coordinate of car 1 is c, then once c + 2 − 1 > 6, the car is considered to have left the parking lot. Each move consists of moving a car one square in one of the allowed directions. Only car 1 is permitted to leave; all other vehicles must remain entirely within the 6×6 grid.

Determine the minimum number of moves needed to allow your car (car 1) to exit the parking lot. If it is impossible to free your car, output -1.

inputFormat

The first line contains an integer n (the number of cars).

Each of the following n lines contains the description of a car in the following format:

length orientation row col

Here, length is either 2 or 3. orientation is either H (for horizontal) or V (for vertical). For a horizontal car, the given (row, col) is the leftmost cell; for a vertical car, it is the topmost cell. Note that car 1 (the first car in the input) is always your car, it is always a 2×1 car, and it is placed on row 3.

outputFormat

Output a single integer — the minimum number of moves needed for your car to exit the parking lot. If it is impossible, output -1.

sample

1
2 H 3 5
1

</p>