-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
332 lines (274 loc) · 9.37 KB
/
map.cpp
File metadata and controls
332 lines (274 loc) · 9.37 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include "map.h"
#include <fstream>
using namespace std;
Map::Map(int num_stores) {
//make all distances 0
for(int i = 0; i < num_stores + 1; i++) {
for(int j = 0; j < num_stores; j++) {
arr[i][j] = 0;
}
}
}
void Map::prompt(int num_stores) {
string s;
cout << "Enter a file that has your map info or type 'manual' to manually enter the map now: ";
cin >> s;
if(s == "manual") {
fill_map_cmdLine(); //enter map through cmd line
} else { //enter map through txt file
bool cmd_line_file_success = handleMapFile(s);
while (!cmd_line_file_success) {
cout << "Enter a file that has your map info or type 'manual' to manually enter the map now: ";
cin >> s;
cmd_line_file_success = handleMapFile(s);
}
cout << "Map is filled!" << endl;
}
cout << endl;
cout << "--------------------------------------------------------------------------" << endl;
cout << "Now you are the customer." << endl;
cout << "You have decided to take your date to the fanciest shopping mall there is. " << endl;
cout << "But you need to look like you know what you're doing, better not get lost..." << endl;
cout << "Time to use NaviShop!" << endl;
start_experience(num_stores);
}
void Map::start_experience(int num_stores) {
int start;
cout << "Which store are you near now? ";
cin >> start;
dijkstra(start, num_stores);
}
bool Map::handleMapFile(string filename) {
// check if file was properly opened using is_open
// if file doesn’t exist or you don’t have permission to open file
ifstream instream;
instream.open(filename);
if (not instream.is_open()) { //check if able to open file
cerr << "Error: Unable to read: " << filename << endl;
return false;
} else {
while(not instream.eof()) {
fill_map_from_file(instream);
}
}
instream.close();
return true;
}
void Map::fill_map_cmdLine() {
bool quit = false;
string cont;
int start;
int end;
int length;
//command line input
while(!quit) {
cout << "Add store? ";
cin >> cont;
if(cont == "yes") {
cout << "Start: ";
cin >> start;
cout << "End: ";
cin >> end;
cout << "Length: ";
cin >> length;
arr[start][end] = length;
arr[end][start] = length;
cout << endl;
} else {
quit = true;
}
}
}
void Map::fill_map_from_file(std::istream &in) {
string s;
int length = 0;
in >> s;
int start = int(s[0]) - 48;
int end = int(s[2]) - 48;
if(s.length() > 3) {
length = string_to_int(s.substr(4));
}
arr[start][end] = length;
arr[end][start] = length;
}
int Map::string_to_int(string s) {
//declare and initialize variable that will integer values from strings
//will be added to
int new_num = 0;
//loop through each character in string
for (size_t i = 0; i < s.length(); ++i) {
//test if the character is an integer and if so, add it to new_num
if (s[i] >= 48 and s[i] < 58) {
new_num = new_num * 10 + (int(s[i]) - 48);
}
}
return new_num;
}
void Map::print_map(int num_stores) {
for(int i = 0; i < num_stores + 1; i++) {
for(int j = 0; j < num_stores; j++) {
if(arr[i][j] != 0) {
cout << "Distance from store " << i << " and store " << j << " is " << arr[i][j] << endl;
}
}
}
}
void Map::find_path(int start, int end, int num_stores, int path) {
//find 1st nonzero value
int col = find_1st_nonzero_col(start, num_stores);
//if value's col != end
if(col != end) {
cout << col << ", ";
path = path + arr[start][col];
find_path(col, end, num_stores, path);
} else if(col == end) {
path = path + arr[start][col];
cout << col << " is the path " << endl;
return;
} else { //repeat with next nonzero value
find_path(find_1st_nonzero_col(start + 1, num_stores), end, num_stores, path);
}
}
/*
void Map::find_path_reverse(int start, int end, int num_stores, int path) {
//find 1st nonzero value
int col = find_1st_nonzero_col_reverse(start);
//if value's col != end
if(col != end) {
cout << col << ", ";
path = path + arr[start][col];
find_path_reverse(col, end, num_stores, path);
} else if(col == end) {
path = path + arr[start][col];
cout << col << " is the path with a length of " << path << endl;
return;
} else { //repeat with next nonzero value
find_path_reverse(find_1st_nonzero_col_reverse(start - 1), end, num_stores, path);
}
}*/
int Map::find_1st_nonzero_col(int row, int num_stores) {
//cout << "Calling find_1st_nonzero_col in row " << row << "for " << num_stores << " stores." << endl;
for(int i = row + 1; i < num_stores; i++) {
//cout << "checking " << i << endl;
if(arr[row][i] != 0) {
return i;
}
}
return 0;
}
int Map::find_1st_nonzero_col_reverse(int row) {
//cout << "Calling find_1st_nonzero_col in row " << row << "for " << num_stores << " stores." << endl;
for(int i = row - 1; i > 0; i--) {
//cout << "checking " << i << endl;
if(arr[row][i] != 0) {
return i;
}
}
return 0;
}
/*
void Map::printSolution(int dist[], int num_stores)
{
printf("Vertex Distance from Source\n");
int min = INT_MAX;
for (int i = 0; i < num_stores; i++) {
//printf("%d \t\t %d\n", i, dist[i]);
}
int end;
cout << "End: ";
cin >> end;
cout << "The distance of the fastest path to " << end << " is " << dist[end] << endl;
}*/
// Function to print shortest path from source to j using
// parent array
void Map::printPath(int parent[], int j)
{
// Base Case : If j is source
if (parent[j] == -1)
return;
printPath(parent, parent[j]);
cout << j << " ";
}
// A utility function to print the constructed distance
// array
void Map::printSolution(int dist[], int num_stores, int parent[], int src)
{
string ans;
cout << "Do you want to see the map from your perspective to see your options(map), or do you have a specific path in mind(path)? ";
cin >> ans;
int end;
if(ans == "path") {
cout << "What is your destination? ";
cin >> end;
cout << src << " ";
printPath(parent, end);
cout << " is the path you must take with a distance of " << dist[end] << endl;
} else if(ans == "map") {
cout << "Full Map in Reference to You: " << endl;
cout << "Vertex\t Distance\tPath";
for (int i = 1; i <= num_stores; i++) {
printf("\n%d -> %d \t\t %d\t\t%d ", src, i, dist[i],
src);
printPath(parent, i);
}
} else {
cout<< "That wasn't one of the options" << endl;
}
cout << endl;
cout << "Would you like to continue exploring or leave the complex (continue/leave)? ";
cin >> ans;
if(ans == "continue") {
start_experience(num_stores);
} else {
cout << "Okay! Thanks for visiting! Hope your date went well ;) " << endl;
}
cout << endl;
}
int Map::minDistance(int dist[], bool sptSet[], int num_stores)
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 1; v <= num_stores; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void Map::dijkstra(int src, int num_stores)
{
//int V = num_stores;
int dist[num_stores]; // The output array. dist[i] will hold the shortest
// distance from src to i
bool sptSet[num_stores]; // sptSet[i] will be true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
// Parent array to store shortest path tree
int parent[num_stores];
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 1; i <= num_stores; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
parent[i] = -1;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < num_stores; count++) {
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in the first iteration.
int u = minDistance(dist, sptSet, num_stores);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (int v = 1; v <= num_stores; v++) {
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && arr[u][v] && dist[u] != INT_MAX && dist[u] + arr[u][v] < dist[v]) {
parent[v] = u;
dist[v] = dist[u] + arr[u][v];
}
}
}
// print the constructed distance array
printSolution(dist, num_stores, parent, src);
}