Sliding Blocks
Time limit1sMemory limit512 MB
Given n rectangles that slide left and down one at a time, find the final lower-left corner of the last block.
- Level
Medium7 of 10
- Topics
- Simulation, Geometry, Sorting
- Solved
- No attempts yet
Statement
Rectangular blocks are given one by one in the plane. Every block is parallel to the xy-axis. Each block starts at a given location and moves along a given direction, which is always left-downward. A block's location is the coordinate of its lower-left corner.
The movement of a block has two phases.
In the first phase, the block moves from its starting location toward . While it moves, if its bottom side hits the x-axis or another block, its direction changes to leftward and it enters the second phase. If its left side hits the y-axis or another block, its direction changes to downward and it enters the second phase. If its lower-left corner touches the upper-right corner of another block, it may change to either downward or leftward. Downward has higher priority. If downward is blocked by other blocks, the direction changes to leftward. If the upper-left or lower-right corner of the block only touches another block's corner, the block keeps its direction, because a corner contact does not block it.
In the second phase, the block repeats move-left and move-down (or move-down and move-left) for as long as it can move. Once the block enters the second phase, it keeps moving in the same direction as far as possible. When the x-axis, the y-axis, or another block blocks it, it changes direction and keeps moving.
Figure J.1 shows an example of a block's path. The red line is the path, and the orange block is the final position.

Figure J.2 shows the corner cases described above.

For each block, the following information is given.
- The width and height and of the block.
- The starting location .
- The direction of the first movement. See Figure J.3. The block begins at and moves toward .

Given the information on blocks, find the location of the last block after every block has moved one by one according to the rules above. The th block (for ) does not overlap any earlier block at its starting location. Some blocks may not move at all from their starting location because other blocks block them.
Input
The first line contains an integer (), the number of blocks. Each of the next lines contains six integers , , , , , . The width and height satisfy . The starting location satisfies . The direction satisfies .
Output
Print one line with two integers and , the location of the last block after all blocks have moved.