This page is still under construction.

Parts of this page are still being built. What you see may change.

How Big Are the Pockets? (Large)

Time limit5sMemory limit512 MB

Summary
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 xx, yy, zz and ww 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 pp 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 pp and a boundary point directly west of pp.
  • There is a boundary point directly north of pp and a boundary point directly south of pp.

A boundary point is any point the professor walks over, not only the ones with integer coordinates.

Look at the first picture again. Point xx satisfies the first condition, point zz satisfies the second one, and point yy satisfies both. All three are in pockets. Point ww 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 NN. The NN test cases follow.

Each test case describes one walk. It starts with an integer LL, followed by LL pairs "SS TT", where SS is a string of the characters 'L', 'R' and 'F', and TT is how many times SS is repeated.

The input for one test case looks like this.

S1 T1 S2 T2 ... SL TL

The actions taken are T1T_1 copies of S1S_1, then T2T_2 copies of S2S_2, and so on.

The pairs of one test case are not always on the same line, but a string SS is never split across lines. The second test case of the first example shows this.

Limits

  • 1≤N≤1001 \le N \le 100
  • 1≤T1 \le T. The coordinate limit below bounds how large TT 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.
  • 1≤L≤10001 \le L \le 1000
  • The length of each string SS 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 XX is the 1-based test case number and YY 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.

Examples3

  1. Example 1

    Input
    2
    1
    FFFR 4
    9
    F 6 R 1 F 4 RFF 2 LFF 1
    LFFFR 1 F 2 R 1 F 5
    
    Expected output
    Case #1: 0
    Case #2: 4
    
  2. Example 2

    Input
    3
    1
    FRFRFRFR 1
    1
    LFRFRFRF 1
    7
    F 5 R 1 F 1 R 1 F 5 R 1 F 1
    
    Expected output
    Case #1: 0
    Case #2: 0
    Case #3: 0
    
  3. Example 3

    Input
    3
    40
    F 5 L 1 F 1 L 1 F 4 R 1 F 1 R 1 F 4 L 1 F 1 L 1 F 4 R 1 F 1 R 1 F 4 L 1 F 1 L 1 F 4 R 1 F 1 R 1 F 4 L 1 F 1 L 1 F 4 R 1 F 1 R 1 F 4 L 1 F 1 L 1 F 5 L 1 F 9 L 1
    24
    F 1 R 1 F 1 L 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 L 1
    28
    F 1 L 1 F 1 R 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 R 1 F 1 L 1 F 1 L 1 F 6 L 1 F 6 L 1
    
    Expected output
    Case #1: 16
    Case #2: 0
    Case #3: 0