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 (2≤n≤100,000) and d (0<d≤10^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^9≤x,y≤10^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.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more