-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_test3.cpp
More file actions
63 lines (54 loc) · 1.35 KB
/
io_test3.cpp
File metadata and controls
63 lines (54 loc) · 1.35 KB
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
#include <iostream>
#include <vector>
using namespace std;
/*
my position is x. o are coins. Find coins
distance( (2,1) => (1,3)) => 3 (manhattan distance)
-------------------------
| . | x | . | . | . | . |
-------------------------
| . | . | . | . | . | . |
-------------------------
| . | . | . | . | . | . |
-------------------------
| o | . | . | . | . | . |
-------------------------
| . | . | . | . | . | . |
-------------------------
*/
class Point {
public:
int x, y;
Point();
Point(int, int);
};
Point::Point() {}
Point::Point(int x, int y) : x(x), y(y) {}
int getDistance(Point &p1, Point &p2) {
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}
Point closestCoin(Point yourPosition, vector<Point> coinPositions) {
Point result;
if (coinPositions.empty()) {
return Point(-1, -1);
}
int min_dist = getDistance(yourPosition, coinPositions[0]);
result = coinPositions[0];
int dist;
for (auto &p : coinPositions) {
dist = getDistance(yourPosition, p);
if (dist < min_dist) {
min_dist = dist;
result = p;
}
}
return result;
}
int main() {
vector<Point> coinPositions;
coinPositions.push_back(Point(3, 3));
coinPositions.push_back(Point(5, 5));
Point res = closestCoin(Point(1, 1), coinPositions);
cout << res.x << ", " << res.y << endl;
return 0;
}