-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsovler-Nearest-Neighbor.cpp
47 lines (37 loc) · 1.07 KB
/
sovler-Nearest-Neighbor.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
// Nearest Neighbor法 (NN)
// 計算量 O(N^2)
// 精度保証 : O(logN)
#include<bits/stdc++.h>
#include"posIOFile.hpp"
using namespace std;
vector<int> solve(vector<Position>);
int main(int argc, char *argv[]){
assert(argc >= 3);
string inputFile = argv[1];
string outputFile = argv[2];
vector<Position> points;
readFile(inputFile, points);
vector<int> answer = solve(points);
outFile(outputFile, answer);
}
vector<int> solve(vector<Position> points){
int n = points.size();
vector<int> result;
vector<bool> usePoint(n, false);
int nowPoint = 0;
result.push_back(nowPoint + 1);
for(int turn = 0; turn < n - 1; turn++){
double minDist = 1e9;
int nextPoint = -1;
usePoint[nowPoint] = true;
for(int i = 0; i < n; i++){
if(!usePoint[i] && points[nowPoint].dist(points[i]) < minDist){
minDist = points[nowPoint].dist(points[i]);
nextPoint = i;
}
}
nowPoint = nextPoint;
result.push_back(nowPoint + 1);
}
return result;
}