Folding Game
Time limit1sMemory limit128 MB
For each fold sequence on a rectangle, count how many paper layers sit under a given point.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Geometry, Recursion
- Solved
- No attempts yet
Problem
Alice places a rectangular sheet of paper in front of Bob. The paper has width and height and lies flat. Alice then folds the paper times. Each fold is either horizontal or vertical: a horizontal fold leaves a rectangle of the same width but smaller height, and a vertical fold leaves a rectangle of the same height but smaller width.
When the folding is done, Alice presses a finger onto some point of the resulting rectangle and asks: how many layers of paper lie directly beneath her finger?
Input
The input contains several test cases. Each test case begins with a line of three integers
W H N
where and () are the width and height of the paper and () is the number of folds. Both and are even. Each of the next lines contains a letter and a number separated by one space
D K
The capital letter is one of T, B, L, R, telling whether the fold comes from the Top, Bottom, Left, or Right. The number gives the position of the fold, measured from that edge; for instance, if is T, Alice lifts the top edge and folds it downward. always lies on the paper and is even. The final line of each test case contains two integers
X Y
the point where Alice puts her finger, measured from the bottom-left corner: is the distance to the right and is the distance upward. The point is guaranteed to lie on the fully folded paper, and both and are odd. Because , , and every are even, this guarantees that never lies exactly over an edge or a fold. The input ends with a line containing three zeros.
Output
For each test case, output a single integer on its own line: the number of layers of paper at the point . Do not print extra spaces, and do not separate the answers with blank lines.