How Big Are the Pockets? (Large)
Time limit5sMemory limit512 MB
A run-length-encoded turtle walk traces a simple closed lattice polygon; compute the total area of all points outside it that have boundary both east and west or both north and south.
- Level
Hard8 of 10
- Topics
- Geometry, Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
Professor Polygonovich, an honest citizen of Flatland, likes to walk along the integer points of the plane. He starts at the origin in the morning, facing north, and he only ever does one of three things.
- 'F': move forward one unit of length.
- 'L': turn 90 degrees left.
- 'R': turn 90 degrees right.
At the end of the day (yes, it is a long walk) he is back at the origin. Apart from the origin he never visits the same point twice, so his path encloses a polygon. In the picture below the interior of the polygon is blue. The points , , and are explained further down.

Once the professor makes more than four turns the polygon is not convex, so it has pockets.
Warning! The definition of a pocket used here may differ from the one you already know.
The gray area in the next picture is the set of pockets of the polygon.

Formally, a point is in a pocket if it is not inside the polygon and at least one of the following two conditions holds.
- There is a boundary point directly east of and a boundary point directly west of .
- There is a boundary point directly north of and a boundary point directly south of .
A boundary point is any point the professor walks over, not only the ones with integer coordinates.
Look at the first picture again. Point satisfies the first condition, point satisfies the second one, and point satisfies both. All three are in pockets. Point is not in a pocket.
Given the professor's walk, compute the total area of the pockets.
Input
The first line contains the number of test cases . The test cases follow.
Each test case describes one walk. It starts with an integer , followed by pairs " ", where is a string of the characters 'L', 'R' and 'F', and is how many times is repeated.
The input for one test case looks like this.
S1 T1 S2 T2 ... SL TL
The actions taken are copies of , then copies of , and so on.
The pairs of one test case are not always on the same line, but a string is never split across lines. The second test case of the first example shows this.
Limits
- . The coordinate limit below bounds how large can be.
- The concatenated path never changes direction twice in a row, so it contains no 'LL', 'RR', 'LR' or 'RL'. It contains at least one 'F'.
- The path does not cross itself except at its end, and it ends back at the origin.
- The length of each string is between 1 and 32, inclusive.
- The professor never visits a point with a coordinate larger than 3000 in absolute value.
Output
For each test case print one line in the form Case #X: Y, where is the 1-based test case number and is the total area of all pockets. That area is always an integer, so print it as an integer.
Hint
The picture below shows the two walks of the first example.
