-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
300 lines (284 loc) · 8.17 KB
/
main.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
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
#include "grid.cpp"
#include <stdio.h>
#include <ctime>
#include <unordered_map>
#include <map>
using namespace std;
#define WIDTH 20
#define HEIGHT 20
#define CELL_SIZE 30
void binary_tree(grid &test)
{
int rando;
vector<cell*> n;
for(int i=1;i<=test.rows;i++)
{
for(int j=1; j<=test.columns; j++)
{
n.clear();
if(test.verify_bound2(i-1, j)) //north
{
n.push_back(&((test.g)[i-1][j]));
}
if(test.verify_bound2(i, j+1)) //east
{
n.push_back(&((test.g)[i][j+1]));
}
if(n.size() == 0)
{
continue;
}
rando = rand()%(n.size());
((test.g)[i][j]).link(n[rando]);
}
}
/*for(int i=1;i<=test.rows;i++)
{
for(int j=1; j<=test.columns; j++)
{
((test.g)[i][j]).links();
cout << endl;
}
}*/
}
void aldous_broder(grid &test)
{
int rando;
vector<cell*> n;
cell *here = test.random_cell();
cell *neighbor;
int unvisited = test.size()-1;
while(unvisited>0)
{
n.clear();
n = (*here).neighbors();
rando = rand()%(n.size());
neighbor = n[rando];
if(((*neighbor).link_vec).empty())
{
(*here).link(neighbor);
unvisited -= 1;
}
here = neighbor;
}
}
bool find(unordered_map<int, cell*> &unvisited, cell* c)
{
unordered_map<int, cell*>:: iterator itr;
for(itr = unvisited.begin(); itr != unvisited.end(); ++itr)
{
if((*(itr->second)).isEqual(c))
{
return true;
}
}
return false;
}
int contains(vector<cell*> path, cell *c)
{
for(int i=0; i<path.size(); i++)
{
if((*(path[i])).isEqual(c))
{
return i;
}
}
return -1;
}
int find_index(unordered_map<int, cell*> &unvisited, cell* c)
{
unordered_map<int, cell*>:: iterator itr;
for(itr = unvisited.begin(); itr != unvisited.end(); ++itr)
{
if((*(itr->second)).isEqual(c))
{
return itr->first;
}
}
}
void stamp(vector<cell*> &path)
{
for(int i=0; i < path.size();i++)
{
cout << (*path[i]).row << (*path[i]).column << "|";
}
cout << endl;
}
void wilsons(grid &test)
{
int rando;
unordered_map<int, cell*> unvisited;
vector<cell*> path;
int a = 1;
for(int i=1;i<=test.rows;i++)
{
for(int j=1; j<=test.columns; j++)
{
unvisited.insert({a, &((test.g)[i][j])});
a++;
}
}
rando = (rand()%(unvisited.size()))+1;
cell *here = unvisited[rando];
unvisited.erase(rando);
int n;
while(!unvisited.empty())
{
//scelgo una cella casuale dalle unvisited
unordered_map<int, cell*>::iterator it_rand = unvisited.begin();
advance(it_rand, rand()%unvisited.size());
here = it_rand -> second;
/*rando = (rand()%(unvisited.size()))+1;
cell *here = unvisited[rando];*/
path.clear();
path.push_back(here);
//int key = ((((*here).row)-1)*test.columns) + ((*here).column);
while(find(unvisited, here))
{
here = (*here).rand_neighbor();
if((n = contains(path, here))>=0)
{
path.resize(n+1);
path.shrink_to_fit();
//cout << "RESET PATH con " << (*here).row<< " "<< (*here).column << endl;
}
else
{
path.push_back(here);
//stamp(path);
}
}
//cout << "CREAZIONE LINKS" << endl;
for(int i=0; i < path.size()-1; i++)
{
(*path[i]).link(path[i+1]);
unvisited.erase(find_index(unvisited, path[i]));
}
}
}
void recursive_backtraker(grid &test)
{
vector<cell*> stack;
vector<cell*> neighbors;
vector<cell*> unvisited_neighbors;
cell *here = test.random_cell();
cell *neighbor;
int initial_size;
stack.push_back(here);
while(!stack.empty())
{
neighbors.clear();
unvisited_neighbors.clear();
here = stack.back();
neighbors = here->neighbors();
initial_size = neighbors.size();
for(int i=0; i<initial_size; i++)
{
if((neighbors[i]->link_vec).empty())
{
unvisited_neighbors.push_back(neighbors[i]);
}
}
if(unvisited_neighbors.empty())
{
stack.pop_back();
}
else
{
neighbor = unvisited_neighbors[rand()%unvisited_neighbors.size()];
here->link(neighbor);
stack.push_back(neighbor);
}
}
}
void kruscal(grid &test)
{
vector<pair<cell*,cell*>> couples;
//vector<cell*> tmp;
pair<cell*, cell*> tmp;
unordered_map<cell*, int> set_for_cell;
multimap<int, cell*> cells_in_set;
vector<cell*> losers;
//INIZIALIZZAZIONE GRID
int a = 1;
for(int i=1;i<=test.rows;i++)
{
for(int j=1; j<=test.columns; j++)
{
set_for_cell.insert({&((test.g)[i][j]), a});
cells_in_set.insert({a, &((test.g)[i][j])});
a++;
}
}
//RIEMPIMENTO COUPLES
for(int i=1;i<=test.rows;i++)
{
for(int j=1; j<=test.columns; j++)
{
//tmp.clear();
if(test.g[i][j].south->row > 0 && test.g[i][j].south->column > 0)
{
tmp = make_pair(&(test.g[i][j]), test.g[i][j].south);
//tmp.push_back(&(test.g[i][j]));
//tmp.push_back((test.g[i][j]).south);
couples.push_back(tmp);
}
//tmp.clear();
if(test.g[i][j].east->row > 0 && test.g[i][j].east->column > 0)
{
tmp = make_pair(&(test.g[i][j]), test.g[i][j].east);
//tmp.push_back(&(test.g[i][j]));
//tmp.push_back((test.g[i][j]).east);
couples.push_back(tmp);
}
}
}
//INIZIO ALGORITMO
//tmp.clear(); //ora viene utilizzato per estrarre le coppie
int winner;
int loser;
while(!couples.empty())
{
vector<pair<cell*,cell*>>::iterator it_rand = couples.begin();
advance(it_rand, rand()%couples.size());
tmp = *it_rand;
couples.erase(it_rand);
if(set_for_cell[tmp.first] != set_for_cell[tmp.second])
{
tmp.first->link(tmp.second);
winner = set_for_cell[tmp.first];
loser = set_for_cell[tmp.second];
//RIEMPIO LOSERS
losers.clear();
multimap<int,cell*>::iterator itr;
pair<multimap<int, cell*>::iterator, multimap<int, cell*>::iterator> ret;
ret = cells_in_set.equal_range(loser);
for (itr=ret.first; itr!=ret.second; ++itr)
{
losers.push_back(itr->second);
}
//AGGIORNO LE DUE MAPPE
for(int i=0; i<losers.size(); i++)
{
//esempio se winner=1 e loser=2 e losers=a,b,c
cells_in_set.insert({winner, losers[i]}); //inserisco a,b,c in cells_in_set con chiave 1
set_for_cell.erase(losers[i]); //elimino a,b,c da set_for_cell
set_for_cell.insert({losers[i], winner}); //reinserisco a,b,c in set_for_cell con valore 1 al posto di 2
}
cells_in_set.erase(loser); //elimino gli elementi con chiave 2 da cells_in_set
}
}
}
int main()
{
srand(time(NULL));
grid test(HEIGHT, WIDTH);
//binary_tree(test);
//aldous_broder(test);
//wilsons(test);
//recursive_backtraker(test);
kruscal(test);
//test.display_grid();
test.display_grid_sfml(WIDTH, HEIGHT, CELL_SIZE);
return 0;
}