Calculate the Fence Needed
InterviewTime limit1sMemory limit128 MB
Given the coordinates of N unit squares, compute the perimeter of the combined farm shape.
Problem
The great farmer has decided to build a fence around his farm. The farm is a group of unit squares on a grid joined edge to edge, and it has no holes inside. The farmer wants to know how long the fence must be to surround the whole farm.
Given the positions of all the unit squares, compute the perimeter of the farm. For example, a farm built from 3 unit squares in an L shape has perimeter 8.
Input
The input holds several test cases. Each test case starts with a line containing one integer (), the area of the farm. Each of the next lines contains two space separated integers and (), the coordinates of the lower left corner of one unit square of the farm. The input ends with a line containing a single 0, and that line must not be processed.
Output
For each test case, print one integer, the perimeter of the farm. Write the result of the th test case on the th line of the output.