People in a certain island want a modern airport. This island has the shape of a
square with side length L (1 <= L <= 10). Its top left corner is at X, Y
(0 <= X, Y <= 50) and the right bottom corner is at X+L, Y+L. They have a
list of N (0 <= N <= 20) existing airports and their locations which they
need to visit frequently. These may be
situated inside this island or outside. Any number of airports can be situated
at the same coordinate position.
The aircraft fly only along grid lines for safety reasons. For example the distance
between (4, 6) and (7, 5) is 7-4+6-5 = 4. The top left corner of the grid is 0,
0. X axis increases from 0, left to right and Y axis increases from top to
bottom from 0. All the airports including the new one are located on grid
points. The total cost of traveling is the sum of the distances to all the other
airports from the new airport. They want the new airport to be located so
that the total cost of traveling is minimized.
Your
task is to find the total cost T of traveling when the airport is built at the
location where the total cost is minimized.
Input (Standard
Input)
1. First Line will be four integers X, Y, L and N.
2. Each of the next N Lines will contain two integers x, y (0 <= x, y
<= 100), the coordinates of an existing airport.
Output (Standard
Output)
Example Input
2 3 2 4
0 0
4 3
5 5
3 4
Example Output
12
1,319 Views |
My answer got mingled
Assuming X,Y are the top left corners
Average the x & y coordinates of all the air ports call them Ax and Ay
then
if (Ax is less then X) then Ax = x
if (Ax is greater then (X+L)) then Ax=X+L
if( Ay is less then Y) then Ay=y
if (Ay is greater then (Y+L)) then Ay=Y+L
Ax,Ay are the coordinates of the airport &
the distance of all the points from this point is the minimum distance
Leave an Answer/Comment