TopCoder problem "Fences" used in TCCC '04 Round 3 (Division I Level One)

Problem Statement

    Note to plugin users: This problem statement contains images.

Farmer John has several pastures, each of which can be described as several overlapping and/or touching rectangles. For instance, the pasture in the picture below can be considered as the union of the rectangles (2,2)-(10,5) and (7,1)-(9,7). Two rectangles that only touch each other in a corner aren't counted as touching or overlapping rectangles, see example 4.

Farmer John wants to enclose all his pastures with fences (one fence for each pasture). Help him find out the total length of all fences if all the borderlines must be fenced (see example 2).

Create a class Fences containing the method fenceLength that takes a int[] x1, a int[] y1, a int[] x2 and a int[] y2, describing the rectangles that make up the pastures by overlapping and/or touching, and returns an int, the total length of all fences. Element i in x1, y1, x2 and y2 describe one rectangle with the opposite corners x1,y1 and x2,y2.



Parameters:int[], int[], int[], int[]
Method signature:int fenceLength(int[] x1, int[] y1, int[] x2, int[] y2)
(be sure your method is public)


-x1, y1, x2 and y2 will contain between 1 and 50 elements, inclusive.
-x1, y1, x2 and y2 will all contain the same number of elements.
-Each element in x1, y1, x2 and y2 will be between 0 and 1000, inclusive.
-Element i in x1 will differ from element i in x2.
-Element i in y1 will differ from element i in y2.


Returns: 60

Here we have four rectangles that together describes two pastures, as rectangles 0 and 1 intersect and rectangles 1 and 2 touch each other.

Returns: 200

x2 and y2 might be greater than x1 and y1.

Returns: 160

These four rectangles form a single pasture with a hole in the middle. Since there must be fences along the borders, there must both be an "outer" fence (length 120) and an "inner" fence (length 40).

 525,260,356,435,369,942, 59,605,173,
 178, 70,420, 40,705, 38,841,430,965,
{314,759, 67,232,815,882,211,839, 48,
 531,567,803,439,418,968, 74, 88,834,
  68,421,659,653, 99,921,339,865,408}
{835,794,980, 90,808,649,198,334,309,
 167,996,212,355, 34,430,749,527,109,
 501,380, 36,641,872, 47,157,433, 85}
 576,695,102,655,121, 82,400, 36,236,
 143,108,862,400,357,386,790,732,  4,
 800,316,166,239,242,121, 57,111, 85}
Returns: 3854
Returns: 36
The two rectangles only touch each other at one corner, and are thus considered as different pastures.

Problem url:

Problem stats url:




lbackstrom , schveiguy

Problem categories:

Brute Force, Geometry