-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW1.cpp
118 lines (95 loc) · 3.27 KB
/
HW1.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cmath>
#include<algorithm>
#include<ctime>
#include "gnuplot-iostream.h"
using namespace std;
vector<string> openFile();
vector<int> splitString(string, char);
void TSP(vector<vector<int>>, vector<int>, double, int, int, vector<int>);
double calculateDistance(int, int, int, int);
double minVal = 30000;
vector<int> minPath;
int main() {
vector<string>data = openFile();
vector<vector<int>> intDataMatrix;
for(int i = 0; i < data.size(); i++) {
intDataMatrix.push_back(splitString(data[i], ' '));
}
vector<int> notYetPass;
for(int i = 1; i < data.size(); i++)
notYetPass.push_back(i);
vector<int> path;
path.push_back(0);
std::clock_t c_start = std::clock();
TSP(intDataMatrix, notYetPass, 0, intDataMatrix[0][1], intDataMatrix[0][2], path);
std::clock_t c_end = std::clock();
for(int i = 0; i < minPath.size(); i++) { // the path
cout << minPath[i]+1 << "->";
}
cout << endl << minVal << endl;
cout << "consuming time: " << (c_end-c_start)/ CLOCKS_PER_SEC << " /s" << endl;
return 0;
}
vector<string> openFile() {
vector<string> data;
ifstream inputFile("readfile.txt");
if (inputFile.is_open())
{
int i = 0;
string line;
while (getline(inputFile,line, '\n'))
{
data.push_back(line);
i++;
}
inputFile.close();
}
return data;
}
vector<int> splitString(string strToSplit, char splitChar) {
vector<int> splitInt;
string tmpS;
int tmpI = 0;
for(int i = 0; i < strToSplit.length(); i++) {
if(strToSplit[i] == splitChar) {
tmpS = strToSplit.substr(tmpI, i-tmpI);
splitInt.push_back(stoi(tmpS, nullptr));
tmpI = i+1;
}
else if(i == strToSplit.length()-1) {
tmpS = strToSplit.substr(tmpI, i-tmpI+1);
splitInt.push_back(stoi(tmpS, nullptr));
}
}
return splitInt;
}
void TSP(vector<vector<int>> intDataMatrix, vector<int> notYetPass, double sum, int prevX, int prevY, vector<int>path) {
if(notYetPass.size() == 0) {
sum += calculateDistance(prevX, prevY, intDataMatrix[0][1], intDataMatrix[0][2]);
path.push_back(0);
if(sum < minVal) {
minVal = sum;
minPath = path;
}
}
else {
for(int i = 0; i < notYetPass.size(); i++) {
double distance = calculateDistance(prevX, prevY, intDataMatrix[notYetPass[i]][1], intDataMatrix[notYetPass[i]][2]);
path.push_back(notYetPass[i]);
int tmp = notYetPass[i];
vector<int>::iterator iter = find(notYetPass.begin(), notYetPass.end(), notYetPass[i]);
notYetPass.erase(iter);
TSP(intDataMatrix, notYetPass, sum+distance, intDataMatrix[tmp][1], intDataMatrix[tmp][2], path);
/* retrieve the value */
notYetPass.push_back(tmp);
path.pop_back();
}
}
}
double calculateDistance(int startX, int startY, int endX, int endY) {
return double(sqrt(pow(startX-endX, 2)+pow(startY-endY, 2)));
}