All problems

Full K=3 Classification

hardPythonK-Nearest Neighbors

Marlow Utilities now wants the shortlist built and voted on in one go. A new fault arrives as a position on the map. The archive holds past faults, each with its own position and the repair category it turned out to need. House policy consults exactly the three archived faults lying nearest the new one as the crow flies, and books whichever category the most of those three needed. Three is fixed by policy, whatever the size of the archive.

Task: Print the repair category the three nearest archived faults point to.

Input

The first line holds the new fault's two map coordinates, separated by a space. The second line holds n, the number of archived faults, always at least 3. Each of the next n lines holds an archived fault's two coordinates followed by its category, separated by single spaces. A category contains no spaces, and more than two of them may appear. Coordinates may be negative or fractional.

Output

One line holding the predicted category. All three consulted faults carry the same weight, however near or far. If two or more categories are named equally often, print whichever comes first alphabetically. If faults tie on distance and taking them all would overfill the three, keep the ones written earlier.

Example:

Input:
0 0
5
1 0 A
0 2 B
2 1 A
3 3 B
0 5 B

Output:
A

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…