-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.cpp
365 lines (293 loc) · 7.57 KB
/
Maze.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// Created by viniman on 10/06/19.
//
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
#include <sstream>
#include "Maze.h"
#include <cmath>
#define HAS_RAND_WEIGHT false
#define VALUE_MAX_RAND_WEIGHT 200
Maze::Maze(string path)
{
ofstream outFile;
ifstream inFile;
string inFileName;
string outFileName;
int originId, destinationId;
inFile.open(path);
if (inFile)
{
cout << "\tLendo arquivo instancia" << endl;
cout << "\tPath (caminho): " << path << endl;
// get length of file:
inFile.seekg(0, std::ifstream::end);
auto length = static_cast<unsigned int>(inFile.tellg());
inFile.seekg(0, std::ifstream::beg);
//criando e inicializando buffer
//buffer contem o arquivo inteiro
auto *buffer = new char[length];
inFile.read(buffer, length);
inFile.close();
stringstream stream;
cout << "\tLendo Instancia\n";
char *p = strtok(buffer, "\n");
stream << p;
stream >> this->mazeLines >> this->mazeColumns >> originId >> destinationId;
initiateWeightMatrix(HAS_RAND_WEIGHT, VALUE_MAX_RAND_WEIGHT);
this->numRooms = this->mazeLines * this->mazeColumns;
rooms = vector<Node*>(numRooms, nullptr);
for (int i = 0; i < rooms.size(); ++i)
{
rooms[i] = new Node(i); // calcular valor heuristico aqui e passar no construtor
}
this->origin = rooms[originId];
this->destination = rooms[destinationId];
calculaXY();
for(auto& it : rooms)
it->setHeuristicValue(manhattanDistance(it));
unsigned int room1, room2;
char direction;
while (p != nullptr)
{
p = strtok(nullptr, "\n");
stream.clear();
stream << p;
stream >> room1 >> room2;
addDoor(room1, room2);
//weightMatrix[room1/mazeColumns][room2%mazeColumns] = 1;
//weightMatrix[room2/mazeColumns][room1%mazeColumns] = 1;
}
delete[] buffer;
return;
}
cout << "\tFalha na leitura do arquivo!" << endl;
cout << "\tO caminho tentado foi: \"" << path << "\"." << endl;
cout << "\tVERIFIQUE se digitou o nome do diretorio path corretamente." << endl;
exit(-1);
}
/**
* Construtor de labirintos com suas dimensões, pondendo setar para adicionar as portas ou não
* @param m
* @param n
* @param addDoors
* @param origin
* @param dest
*/
Maze::Maze(unsigned int m, unsigned int n, bool addDoors, unsigned int origin, unsigned int dest)
{
mazeLines = m;
mazeColumns = n;
numRooms = m*n;
//rooms = vector<Node*>(numRooms, nullptr);
for(int i = 0; i < numRooms; ++i)
{
//Node* node = new Node(i);
//rooms[i] = new Node(i);
rooms.push_back(new Node(i));
}
initiateWeightMatrix(HAS_RAND_WEIGHT, VALUE_MAX_RAND_WEIGHT);
//for(auto i : rooms)
//cout << i->getId() << ", ";
cout << endl;
/* for(unsigned int room1 = 0, room2 = 0; room1 < n-1; ++room1)
{
room2 = room1+1;
addDoor(room1, room2);
for(unsigned int room2 = room1; room2 < n-1; ++room2)
{
room1*=10;
room2 = room1+10;
addDoor(room1, room2);
}
}*/
calculaXY();
// Necessario para o algoritmo de geracao de labirinto (cria todas as portas)
if(addDoors)
{
//Adiciona portas na horizontal
for (unsigned int i = 0; i < n*m-1; ++i)
{
if(i%n != n-1)
addDoor(i, i+1);
}
//Adiciona portas na vertical
for (unsigned int i = 0; i < (n-1)*m; ++i)
{
addDoor(i, i+n);
}
/*for (unsigned int room2 = room1; room2 < m; ++room2)
{
if(room2/n) //n
addDoor(room2, room2 + 1);
//if(room2<=m*n-n) //m
addDoor(room2, room2 + n);
}*/
}
/* else
{
for(auto& it : rooms)
it->setHeuristicValue(manhattanDistance(it));
}*/
}
Maze::~Maze()
{
//liberar memória
for(int i = 0;i < this->mazeLines; ++i)
delete [] weightMatrix[i];
delete [] weightMatrix;
}
void Maze::initiateWeightMatrix(bool randomizedWeight, unsigned int maxRandValue)
{
this->weightMatrix = new int*[mazeLines];
for (int i = 0; i < mazeColumns; ++i)
{
this->weightMatrix[i] = new int[mazeColumns];
}
for(int i = 0;i < mazeLines; ++i)
{
for(int j = i;j < mazeColumns; ++j)
{
if(randomizedWeight)
{
int randValue = rand()%maxRandValue;
weightMatrix[i][j] = randValue;
weightMatrix[j][i] = randValue;
}
else
{
weightMatrix[i][j] = 1;
weightMatrix[j][i] = 1;
}
}
}
}
const vector<Node *> &Maze::getRooms() const
{
return rooms;
}
void Maze::calculaXY()
{
/*
int i, j;
auto it = rooms.begin();
*/
for(auto& itRoom: rooms)
{
itRoom->setX(itRoom->getId()/mazeColumns);
itRoom->setY(itRoom->getId()%mazeColumns);
}
/*
for(i = 0; i < mazeLines; ++i)
{
for(j = 0; j < mazeColumns; ++j)
{
(*it)->x = i;
(*it)->y = j;
if((i * mazeColumns + j + 1) >= numRooms)
return;
++it;
}
}*/
}
// |x2 - x1| + |y2 - y1|
int Maze::manhattanDistance(Node *no1)
{
return abs(destination->getX() - no1->getX()) + abs(destination->getY() - no1->getY());
}
char Maze::operacao(Node *no1, Node *no2)
{
int x1 = no1->getX();
int y1 = no1->getY();
int x2 = no2->getX();
int y2 = no2->getY();
if((x1 == x2) && (y1+1 == y2)) //Verifica se no2 está a direita de no1
{
return 'R';
}
else if((x1 == x2) && (y2+1 == y1)) //Verifica se no2 está a Esquerda de no1
{
return 'L';
}
else if((y1 == y2) && (x1 - 1 == x2)) //Verifica se no2 está a acima de no1
{
return 'T';
}
else if((y1 == y2) && (x1 + 1 == x2)) //Verifica se no2 está a Embaixo de no1
{
return 'B';
}
else //Erro
{
exit(-1);
}
}
unsigned int Maze::getMazeLines() const
{
return mazeLines;
}
unsigned int Maze::getMazeColumns() const
{
return mazeColumns;
}
unsigned int Maze::getNumRooms() const
{
return numRooms;
}
Node *Maze::getOrigin() const
{
return origin;
}
Node *Maze::getDestination() const
{
return destination;
}
Node * Maze::getRoom(int id) const
{
return rooms[id];
}
void Maze::setVisitedAllFalse()
{
for(auto& it : rooms)
it->setNonVisited();
}
void Maze::addDoor(unsigned int room1, unsigned int room2)
{
switch (operacao(rooms[room1],rooms[room2]))
{
case 'R':
{
rooms[room1]->setRight(rooms[room2]);
rooms[room2]->setLeft(rooms[room1]);
break;
}
case 'L':
{
rooms[room1]->setLeft(rooms[room2]);
rooms[room2]->setRight(rooms[room1]);
break;
}
case 'B':
{
rooms[room1]->setBotton(rooms[room2]);
rooms[room2]->setTop(rooms[room1]);
break;
}
case 'T':
{
rooms[room1]->setTop(rooms[room2]);
rooms[room2]->setBotton(rooms[room1]);
break;
}
}
}
void Maze::setDestination(unsigned int id)
{
destination = rooms[id];
}
void Maze::setOrigin(unsigned int id)
{
origin = rooms[id];
}