-
Notifications
You must be signed in to change notification settings - Fork 1
/
142.cc
83 lines (77 loc) · 2.48 KB
/
142.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
enum {
MIN_COORDINATE = 0,
MAX_COORDINATE = 499,
COORDINATE_RANGE = MAX_COORDINATE - MIN_COORDINATE + 1,
};
int ToIndex(int coordinate) { return coordinate - MIN_COORDINATE; }
int ComputeDistance(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main() {
char regions[COORDINATE_RANGE][COORDINATE_RANGE];
char region = 'A';
memset(regions, 0, sizeof(regions));
std::vector<std::pair<int, int>> icons; // (x, y)
std::vector<std::pair<int, int>> distances; // (index, distance)
auto less_than = [](const std::pair<int, int>& d1,
const std::pair<int, int>& d2) {
return d1.second < d2.second ||
(d1.second == d2.second && d1.first < d2.first);
};
std::string type;
while (std::cin >> type && type != "#") {
if (type == "I") {
int x, y;
std::cin >> x >> y;
icons.push_back(std::make_pair(x, y));
} else if (type == "R") {
int x1, y1, x2, y2;
std::cin >> x1 >> y1 >> x2 >> y2;
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
regions[ToIndex(x)][ToIndex(y)] = region;
}
}
region++;
} else if (type == "M") {
int mouse_x, mouse_y;
std::cin >> mouse_x >> mouse_y;
if (regions[ToIndex(mouse_x)][ToIndex(mouse_y)] != '\0') {
std::cout << regions[ToIndex(mouse_x)][ToIndex(mouse_y)] << std::endl;
} else {
distances.clear();
for (auto i = icons.cbegin(); i != icons.cend(); ++i) {
int x = i->first;
int y = i->second;
if (regions[ToIndex(x)][ToIndex(y)] == '\0') {
int index = i - icons.cbegin();
int distance = ComputeDistance(mouse_x, mouse_y, x, y);
distances.push_back(std::make_pair(index, distance));
}
}
assert(!distances.empty()); // At least one visible icon.
std::sort(distances.begin(), distances.end(), less_than);
int distance = distances.front().second;
for (auto i = distances.cbegin(); i != distances.cend(); ++i) {
if (i->second != distance) {
break;
}
char buffer[8];
snprintf(buffer, sizeof(buffer), "%3d", i->first + 1);
std::cout << buffer;
}
std::cout << std::endl;
}
} else {
assert(false);
}
}
return 0;
}