-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
393 lines (339 loc) · 8.93 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class Vertice{
public:
int indice;
string nome;
bool visitado = false;
int heuristica = 0;
int aux_heu =0;
int g_de_n =0;
vector<Vertice*> adj;
vector<int> adjDist;
public:
Vertice(){
}
Vertice(int indice,string nome){
this-> indice = indice;
this-> nome = nome;
}
string getNome(){
return this->nome;
}
int getIndice(){
return this->indice;
}
void setVisitado(){
this->visitado = true;
}
bool getVisitado(){
return this->visitado;
}
void printVertice(){
cout << indice << " " << nome << " " << visitado << endl;
}
void addAdj(Vertice *v1, int dist){
this->adj.push_back(v1);
this->adjDist.push_back(dist);
}
Vertice* getAdj(int pos){
return this->adj[pos];
}
int getAdjDist(int pos){
return this->adjDist[pos];
}
void setHeuristica(int heuristica){
this->heuristica = heuristica;
}
int getHeuristca(){
return this->heuristica;
}
int getG_de_N(){
return this->g_de_n;
}
void setG_de_N(int g_de_n){
this->g_de_n = g_de_n;
}
int getAux_Heu(){
return this->aux_heu;
}
void setAux_Heu(int aux_heu){
this->aux_heu = aux_heu;
}
};
class Grafo{
public: queue<Vertice*> fila;
public: queue<Vertice*> borda;
public: vector<Vertice*> explorados;
public: vector<Vertice*> bordaA;
public:
Grafo(){
}
void ordernarBordaA(){
int maior = 0, ind_maior = 0;
int tam = bordaA.size();
vector<Vertice*> aux_vector = bordaA;
bordaA.clear();
for (int i = 0; i < tam; i++){
maior=0;
for(int j = 0; j < aux_vector.size(); j++){
if( aux_vector[j]->getHeuristca() > maior){
maior = aux_vector[j]->getHeuristca();
ind_maior = j;
}
}
bordaA.push_back(aux_vector[ind_maior]);
swap(aux_vector[ind_maior],aux_vector.back());
aux_vector.pop_back();
}
}
void bfs(Vertice *inicial, Vertice *final){
Vertice *atual = inicial;
atual->setVisitado();
cout << "Iniciando Busca de rota " << endl;
cout << "Rota: de Cidade " << inicial->getNome() << " ate Cidade " << final->getNome() << endl;
cout << "Iniciando a partir da cidade " << inicial->getNome() << endl;
while(!final->getVisitado()){
for(int i = 0 ; i < atual->adj.size(); i++){
if(!atual->getAdj(i)->getVisitado()){
cout << "Vistando cidade " << atual->getAdj(i)->getNome() << endl;
atual->getAdj(i)->setVisitado();
fila.push(atual->getAdj(i));
if(final->getVisitado())
break;
}
}
if(!fila.empty()){
atual = fila.front();
fila.pop();
}else{
break;
}
}
cout<< "Chegou ao seu destino! " <<endl;
queue<Vertice*> fila_ex = fila;
cout << "Borda: ";
while(!fila_ex.empty()){
cout << fila_ex.front()->getNome() << ", ";
fila_ex.pop();
}
cout << endl;
}
bool contido_borda(Vertice* v1, queue<Vertice*> borda){
queue <Vertice *> fila_aux = borda;
for(int i = 0; i < borda.size(); i++){
if(v1 == fila_aux.front())
return true;
else
fila_aux.pop();
}
return false;
}
bool contido_explorados(Vertice* v1, vector<Vertice*> explorados) {
for(int i = 0; i < explorados.size(); i++){
if(v1 == explorados[i])
return true;
}
return false;
}
bool contido_bordaA(Vertice* v1, vector<Vertice*> bordaA) {
for(int i = 0; i < bordaA.size(); i++){
if(v1 == bordaA[i])
return true;
}
return false;
}
bool bordaAComMaiorValor(Vertice * v1, int aux_heu){
for(int i=0; i < bordaA.size(); i++){
if(bordaA[i] == v1){
if(bordaA[i]->getHeuristca() > aux_heu){
return true;
}
}
}
return false;
}
void busca_em_largura(Vertice *inicial, Vertice *final) {
Vertice *atual;
borda.push(inicial);
explorados.clear();
while(true){
cout << endl;
if(borda.empty()){
return;
}
atual = borda.front();
//cout << atual->getNome() << endl;
borda.pop();
explorados.push_back(atual);
for(int i=0; i < atual->adj.size(); i++){
if((contido_explorados(atual->adj[i], explorados) == false) && (contido_borda(atual->adj[i],borda) == false)){
if(atual->adj[i] == final)
return;
else
borda.push(atual->adj[i]);
}
}
}
}
void mostrarExplorado(){
cout << "Explorados: ";
for(int i=0; i < explorados.size(); i ++){
cout << explorados[i]->getNome() << " ";
}
cout << endl;
}
void mostrarBordaA(){
cout << "Borda A: ";
for(int i=0; i < bordaA.size(); i++){
cout << bordaA[i]->getNome() << " ";
}
cout << endl;
}
void mostrarBorda(){
queue<Vertice *> fila_aux = borda;
cout << "Bordas: ";
for(int i = 0; i < borda.size(); i++){
cout << fila_aux.front()->getNome() << " ";
fila_aux.pop();
}
cout << endl;
}
void busca_aEstrela(Vertice *inicial, Vertice *final){
Vertice *atual;
bordaA.push_back(inicial);
explorados.clear();
while(true){
if(bordaA.empty()){
return;
}
atual = bordaA.back();
bordaA.pop_back();
if(atual == final){
return;
}
explorados.push_back(atual);
for(int i=0; i < atual->adj.size(); i++){
if((contido_explorados(atual->adj[i], explorados) == false) && (contido_bordaA(atual->adj[i],bordaA) == false)){
atual->adj[i]->setG_de_N(atual->getG_de_N() + atual->adjDist[i]);
atual->adj[i]->setHeuristica(atual->adj[i]->getIndice() + atual->adj[i]->getG_de_N());
bordaA.push_back(atual->adj[i]);
ordernarBordaA();
}else if(bordaAComMaiorValor(atual->adj[i], atual->getG_de_N() + atual->adjDist[i] + atual->adj[i]->getIndice())){
atual->adj[i]->setG_de_N(atual->getG_de_N() + atual->adjDist[i]);
atual->adj[i]->setHeuristica(atual->adj[i]->getIndice() + atual->adj[i]->getG_de_N());
ordernarBordaA();
}
}
}
}
};
int main(){
//Adicionando Vertices
Vertice *Oradea = new Vertice(380,"Oradea");
Vertice *Zerind = new Vertice(374,"Zerind");
Vertice *Arad = new Vertice(366,"Arad");
Vertice *Timisoara = new Vertice(329,"Timisoara");
Vertice *Lugoj = new Vertice(244,"Lugoj");
Vertice *Mehadia = new Vertice(241,"Mehadia");
Vertice *Drobeta = new Vertice(242,"Drobeta");
Vertice *Craiova = new Vertice(160,"Craiova");
Vertice *Pitesti = new Vertice(100,"Pitesti");
Vertice *RimnicuVilecea = new Vertice(193,"RimnicuVilecea");
Vertice *Sibiu = new Vertice(253,"Sibiu");
Vertice *Fagaras = new Vertice(176,"Fagaras");
Vertice *Bucharest = new Vertice(0,"Bucharest");
Vertice *Giurgiu = new Vertice(77,"Giurgiu");
Vertice *Urziceni = new Vertice(80,"Urziceni");
Vertice *Hirsova = new Vertice(151, "Hirsova");
Vertice *Eforie = new Vertice(161, "Eforie");
Vertice *Vaslui = new Vertice(199, "Vaslui");
Vertice *Iasi = new Vertice(226, "Iasi");
Vertice *Neamt = new Vertice(234, "Neamt");
//Caminhos de Oradea
Oradea->addAdj(Zerind,71);
Oradea->addAdj(Sibiu,151);
//Caminhos de Zerind
Zerind->addAdj(Arad,75);
Zerind->addAdj(Oradea,71);
//Caminhos de Sibiu
Sibiu->addAdj(Fagaras,99);
Sibiu->addAdj(RimnicuVilecea,80);
Sibiu->addAdj(Oradea,71);
Sibiu->addAdj(Arad,140);
//Caminhos de Fagaras
Fagaras->addAdj(Sibiu,99);
Fagaras->addAdj(Bucharest,211);
//Caminhos de Rimnicu Vilecea
RimnicuVilecea->addAdj(Sibiu,80);
RimnicuVilecea->addAdj(Pitesti,97);
RimnicuVilecea->addAdj(Craiova,146);
//Caminhos de Arad
Arad->addAdj(Timisoara,118);
Arad->addAdj(Zerind,75);
Arad->addAdj(Sibiu,140);
//Caminhos de Timisoara
Timisoara->addAdj(Arad,118);
Timisoara->addAdj(Lugoj,111);
//Caminhos de Lugoj
Lugoj->addAdj(Timisoara,111);
Lugoj->addAdj(Mehadia,70);
//Caminhos de Mehadia
Mehadia->addAdj(Lugoj,70);
Mehadia->addAdj(Drobeta,75);
//Caminhos de Dobreta
Drobeta->addAdj(Mehadia,75);
Drobeta->addAdj(Craiova,120);
//Caminhos de Craiova
Craiova->addAdj(Drobeta,120);
Craiova->addAdj(RimnicuVilecea,146);
Craiova->addAdj(Pitesti,138);
//Caminhos de Pitesti
Pitesti->addAdj(Craiova,138);
Pitesti->addAdj(RimnicuVilecea,97);
Pitesti->addAdj(Bucharest,101);
//Caminhos de Bucharest
Bucharest->addAdj(Pitesti,101);
Bucharest->addAdj(Fagaras,211);
Bucharest->addAdj(Giurgiu,90);
Bucharest->addAdj(Urziceni,85);
//Caminhos de Urziceni
Urziceni->addAdj(Bucharest,85);
Urziceni->addAdj(Hirsova,98);
Urziceni->addAdj(Vaslui,142);
//Caminhos de Giurgiu
Giurgiu->addAdj(Bucharest,90);
//Caminhos de Hisorva
Hirsova->addAdj(Urziceni,98);
Hirsova->addAdj(Eforie,86);
//Caminhos de Eforie
Eforie->addAdj(Hirsova,86);
//Caminhos de Vaslui
Vaslui->addAdj(Urziceni,142);
Vaslui->addAdj(Iasi,92);
//Caminhos de Iasi
Iasi->addAdj(Vaslui,92);
Iasi->addAdj(Neamt,87);
//Caminhos de Giugiu
Giurgiu->addAdj(Iasi,87);
// Giurgiu->setHeuristica(500);
// Oradea->setHeuristica(230);
// Iasi->setHeuristica(31);
// Hirsova->setHeuristica(151);
Grafo grafo;
// grafo.bordaA.push_back(Giurgiu);
// grafo.bordaA.push_back(Oradea);
// grafo.bordaA.push_back(Iasi);
// grafo.bordaA.push_back(Hirsova);
// grafo.mostrarBordaA();
// grafo.ordernarBordaA();
// grafo.mostrarBordaA();
// grafo.busca_em_largura(Fagaras, Timisoara);
grafo.busca_aEstrela(Hirsova, Bucharest);
grafo.mostrarExplorado();
// grafo.mostrarBorda();
grafo.mostrarBordaA();
return 0;
}