Packing Rectangles
InterviewTime limit1sMemory limit512 MB
Given four rectangles, find every smallest enclosing axis-parallel rectangle that fits all four without overlap, using the six basic layouts.
- Level
Medium7 of 10
- Topics
- Brute force, Geometry, Implementation, Simulation
- Solved
- No attempts yet
Problem

Figure 1: The six basic layouts of four rectangles
You are given four rectangles. Find the smallest enclosing rectangle into which all four fit without overlapping. "Smallest" means the one with the least area.
Every rectangle must have its sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows the six ways to fit four rectangles together; every other arrangement can be obtained from one of these basic layouts by rotation or reflection, so these six are the only basic layouts.
Each rectangle may be placed in either orientation (rotated by ). Several different enclosing rectangles with the same minimum area may exist, and you must report all of them.
Input
The input consists of four lines. Each line describes one rectangle by two positive integers: the lengths of its two sides. Each side length is between and , inclusive.
Output
Print one line more than the number of solutions. The first line contains a single integer: the minimum area of an enclosing rectangle. Each of the following lines describes one solution by two numbers and with . These lines must be sorted in ascending order of and must all be distinct.