Description
Assume the coasting is an infinite straight line. land is in one side of coasting, sea in the other. each small island is a point locating in the sea side. and any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. the sea side is abve x-axis, and the land side below. given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. note that the position of an island is represented by its x-y coordinates.
Figure A Sample Input of Radar Installations
Input
The input consists of several test cases. the first line of each case contains two integers n (1 <= n <= 1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. this is followed by n lines each containing two integers representing the coordinate of the position of each island. then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
Source
Beijing 2002
Find the valid range of the radar that covers each Island, and then scan the range to remove overlap.
# Include <iostream>
# Include <algorithm>
# Include <vector>
# Include <cmath>
Using namespace std;
Struct Island
{
Int x; // Island x coordinate
Int y; // island y coordinate
Double minRadarX; // covers the left end of the radar effective range.
Double maxRadarX; // the right end of the radar effective range.
};
Bool IslandCmp (Island & island1, Island & island2)
{
Return island1.x <island2.x;
}
Int main ()
{
Int n, d;
Int caseNo = 0;
Vector <Island> islandsVec;
While (cin> n> d)
{
IslandsVec. clear ();
If (n = 0 | d = 0)
Break;
CaseNo ++;
Island island;
For (int I = 0; I <n; I ++)
{
Cin> island. x> island. y;
IslandsVec. push_back (island );
}
// Sort by island x coordinate
Sort (islandsVec. begin (), islandsVec. end (), IslandCmp );
// Calculate the radar effective range corresponding to each island
Bool bNoAnswer = false;
Vector <Island >:: iterator iter;
For (iter = islandsVec. begin (); iter! = IslandsVec. end (); iter ++)
{
If (iter-> y> d)
{
BNoAnswer = true;
Break;
}
Double r = sqrt (d * 1.0-iter-> y * iter-> y );
Iter-> minRadarX = iter-> x-r;
Iter-> maxRadarX = iter-> x + r;
}
If (bNoAnswer)
{
Cout <"Case" <caseNo <":" <-1 <endl;
}
Else
{
// Count the actual number of radars required
Int result = 0;
Double preEnd;
Vector <Island >:: iterator iter;
For (iter = islandsVec. begin (); iter! = IslandsVec. end (); iter ++)
{
If (iter = islandsVec. begin ())
{
Result ++;
PreEnd = iter-> maxRadarX;
}
Else
{
If (iter-> minRadarX> preEnd)
{
Result ++;
PreEnd = iter-> maxRadarX;
}
Else
{
If (iter-> maxRadarX <preEnd)
PreEnd = iter-> maxRadarX;
}
}
}
Cout <"Case" <caseNo <":" <result <endl;
}
}
Return 0;
}