Decorate the Wall
InterviewTime limit1sMemory limit128 MB
Given non-overlapping axis-aligned rectangles on a wall, find the lowest then leftmost position where a new w' by h' rectangle fits without overlapping any of them, or report failure.
- Level
Medium6 of 10
- Topics
- Geometry, Sorting, Binary search, Intervals
- Solved
- No attempts yet
Problem
Mr. Rich has just finished building his enormous villa, and the bare interior walls bother him. He decides to hang paintings from his collection, but it quickly becomes hard to find a spot on a wall where a new painting fits without overlapping the ones already hanging.
Write a program that, given the paintings already on a wall, decides where to hang the next painting without moving any existing painting — or reports that it is impossible.
Every painting is an axis-aligned rectangle whose sides are parallel to the edges of the wall, and paintings may not be rotated.
Input
The first line contains the number of test cases.
Each test case begins with a line containing three integers , , and : the number of paintings already on the wall, the width of the wall, and the height of the wall.
Each of the next lines contains four integers with and . The -coordinates measure the distance from the left edge of the wall and the -coordinates measure the distance from the bottom edge. is the lower-left corner of a painting and is its upper-right corner.
The last line of the test case contains the size of the next painting to hang: its width followed by its height (, ). The painting may not be rotated.
You may assume and . The paintings already on the wall never overlap one another.
Output
For each test case print one line.
If there is no free spot where the new painting fits without overlapping any existing painting, print Fail!.
Otherwise print the coordinates of the lower-left corner where the painting should be placed, as two integers x y separated by a single space. Two paintings that only touch along an edge or at a corner do not count as overlapping. When more than one placement is possible, choose the one with the smallest ; if several placements share that smallest , choose the one with the smallest .