Origami Through-Hole
Time limit1sMemory limit128 MB
Simulate repeated paper folds with layered segments and reflection/overlap propagation rules, then count how many layers a pin punch pierces.
- Level
Hard9 of 10
- Topics
- Geometry, Simulation, Implementation
- Solved
- No attempts yet
Statement
You must simulate folding a flat square sheet of paper and then punching a single pin through the folded stack, and report how many pinholes appear on the original sheet once it is unfolded.
You are given a sequence of folding instructions followed by one pinhole position. A folding instruction is a pair of points and . The paper is folded so that comes down onto from above: the sheet is creased along the fold line — the perpendicular bisector of segment — and the part of the paper on the side is turned over onto the other side. The thickness of the paper is ignored.
The folded paper is a stack of flat paper segments joined along straight hinges (creases). Each fold divides some segments into smaller ones along the fold line and turns some of them over: a segment that crosses the fold line is split, and one of the two new pieces is turned over; a segment that does not cross the fold line is either turned over whole or left as is. Which segments are turned over is decided by repeatedly applying the following rules until no further segment needs turning:
- Rule 1. The uppermost segment that contains must be turned over.
- Rule 2. If turning a segment over moves one of its hinges to the other side of the fold line, then every segment sharing that hinge must also be turned over.
- Rule 3. If two segments overlap and the lower one is turned over, the upper one must be turned over as well.
When a segment is turned over it is reflected across the fold line. After all folding instructions are carried out, the pin is pushed through every layer of paper at the pinhole position, so each layer covering that position receives one hole. Report the number of holes on the sheet when it is unfolded (equivalently, the number of paper layers covering the pinhole position in the final folded stack).
Input
The input is a sequence of datasets. The end of the input is a line containing a single zero. Each dataset has the form
k
px1 py1 qx1 qy1
...
pxk pyk qxk qyk
hx hy
For every dataset the initial sheet is a 100 mm square; using millimetres as the unit, its corners are at , , and . The integer is the number of folding instructions, with . Each of the next lines has four integers , , , separated by spaces, giving the points and of the -th instruction; you may assume . The instructions must be carried out in the given order. The last line has two integers and , the position of the pinhole.
You may assume the following:
- At the time of each fold, and lie on some paper segments, and is at least 0.01 mm away from every border of the paper segments.
- At the time of punching, the pinhole is at least 0.01 mm away from every border of the paper segments.
- Every fold line, extended infinitely in both directions, is at least 0.01 mm away from every corner of the paper segments present just before that fold.
- Whenever two paper segments overlap, the overlapping region cannot be fit between two parallel lines 0.01 mm apart; and whenever two paper segments do not overlap, every point of one is at least 0.01 mm from every point of the other.
Output
For each dataset, output a single line containing the number of pinholes on the unfolded sheet. No other characters may appear in the output.