A common approach is to divide the world into square cells, where the size of the cell is fixed at the maximum distance, d, that a player-sized object would be visible. The cells form a grid, and, because of the carefully chosen grid size, a player in cell (i,j)(i,j) can see, at most, only the objects in that cell and in the neighboring (horizontally, vertically, or diagonally) cells. Objects in any non-neighboring cells would definitely be too far away to be seen.
For example, a player in cell (3,2) would be able to possibly see objects only in that same cell and in the surrounding cells. A player near the left edge of (3,2), for example, would not be able to see objects near the right edge of (4,2). For that matter, a player near the lower-left corner of the (3,2) would not be able to see objects near the upper-right corner of that same cell.
In this program, we want to focus on tracking the characters, i.e., players and NPCs (non-player characters, including animals) in the game. The game mechanics are set up so that players are rarely together in a cell by accident (unless they have teamed up for a quest, and the size of such teams is strictly limited). The world is large enough that, at any given moment, the vast majority of cells will contain no characters at all.
Given a list of n characters coordinates in the world, write a program to count the number of characters that can see at least one other character.
2.1 Input
Input is accepted from the standard input (cin
) or from a file named as a command line parameter..
The input begins with a line with two integers, n (2n100,000) and d (0<d10^9), where n is the number of characters, and d is the maximum distance at which one character can see another.
On each of the following n lines will be two integers xx and y, (10^9x,y10^9) which are the (x,y) coordinates of one character.
No two characters will be at exactly the same (x,y) position.
A scenario beginning with an n of zero (and any integer for d) signals the end of input and is not processed.
2.2 Output
Output is sent to standard out. The program will print a single integer indicating the number of characters that are within a distance d of at least one other character.
2.3 Examples
Input Output
7 2 5
0 0
1 1
1 3
0 5
1000 1000
1001 1002
1001 999
Input Output
7 3 7
0 0
1 1
1 3
0 5
1000 1000
1001 1002
1001 999
Input Output
7 2 5
-1 0
0 1
0 3
-1 5
999 1000
1000 1002
1000 999
Note: Only cellIndex.h
, cellIndex.cpp
, and cellMap.cpp
can be altered.