-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.cpp
52 lines (43 loc) · 1.1 KB
/
1.cpp
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
/*
We are given an array of n points in the plane, and the problem is to
find out the closest pair of points in the array.
*/
#include <bits/stdc++.h>
using namespace std;
class Point
{
public:
int x, y;
};
bool compareX(Point a, Point b) { return a.x < b.x; }
bool compareY(Point a, Point b) { return a.y < b.y; }
float distance(Point a, Point b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
float bruteForce(Point P[], int n)
{
float closestDistance = INT_MAX;
pair<int, int> points;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
float temp = distance(P[i], P[j]);
if (temp < closestDistance)
{
closestDistance = temp;
points = {i, j};
}
}
}
cout << points.first << " " << points.second << endl;
return closestDistance;
}
int main()
{
Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
int n = sizeof(P) / sizeof(P[0]);
cout << "The smallest distance is " << bruteForce(P, n);
return 0;
}