#P2206. Bessie's Dance Stage

    ID: 15486 Type: Default 1000ms 256MiB

Bessie's Dance Stage

Bessie's Dance Stage

Farmer Jhon has built a rectangular stage for Bessie's final report performance. Bessie starts with her four feet on four adjacent 1×1 cells arranged as follows (when facing North):

FL FR
RL RR

Bessie will execute N (1 ≤ N ≤ 1000) dance instructions. Each instruction is a three-character string and can be one of the two types:

  • Movement Instruction: Formatted as XYD, where XY is the foot code and D is one of the directions relative to Bessie's current facing direction:</p>
    • F: forward
    • B: backward
    • R: right
    • L: left

    For example, FRF means that the front right foot moves one unit forward.

  • Pivot Instruction: Formatted as XYP, where XY is the pivot foot. When this instruction is executed, Bessie rotates 90° clockwise around the pivot foot. The pivot foot remains in place, and each of the other three feet is rotated using the transformation \( (x,y) \to (y, -x) \) relative to the pivot. Bessie's facing direction also rotates 90° clockwise.</p>

Throughout the dance, if at any moment two or more feet occupy the same cell, Bessie trips and the performance fails. In that case, output -1.

Otherwise, determine the area of the smallest axis-aligned rectangular stage that covers every cell ever occupied by any of Bessie's feet during her performance. The area is given by \( (\text{max}_x - \text{min}_x + 1) \times (\text{max}_y - \text{min}_y + 1) \).

Note: Tripping by overlapping two feet is the only failure condition; Bessie is otherwise extraordinarily flexible.

inputFormat

The first line contains an integer N (1 ≤ N ≤ 1000), the number of instructions.

Each of the following N lines contains a three-character instruction describing either a movement or pivot operation.

outputFormat

If Bessie trips (i.e. if two feet ever share the same cell), output -1. Otherwise, output a single integer representing the area of the smallest rectangular stage that can contain all positions occupied by her feet.

sample

1
FRF
6

</p>