Skip to content

Commit a470c86

Browse files
committed
terminei bfs mas ta dando erro de execução no hux
1 parent f7acea7 commit a470c86

File tree

4 files changed

+114
-49
lines changed

4 files changed

+114
-49
lines changed

.vscode/c_cpp_properties.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "linux-gcc-x64",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"compilerPath": "/usr/bin/gcc",
9+
"cStandard": "c99",
10+
"cppStandard": "c++11",
11+
"intelliSenseMode": "linux-gcc-x64",
12+
"compilerArgs": [
13+
"-Wall",
14+
"-Wextra",
15+
"-Wpedantic"
16+
]
17+
}
18+
],
19+
"version": 4
20+
}

.vscode/launch.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C/C++ Runner: Debug Session",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"args": [],
9+
"stopAtEntry": false,
10+
"cwd": "/mnt/e/Users/pedro/Desktop/HILUK/DataStructure",
11+
"environment": [],
12+
"program": "/mnt/e/Users/pedro/Desktop/HILUK/DataStructure/build/Debug/outDebug",
13+
"internalConsoleOptions": "openOnSessionStart",
14+
"MIMode": "gdb",
15+
"miDebuggerPath": "/usr/bin/gdb",
16+
"externalConsole": false
17+
}
18+
]
19+
}

.vscode/settings.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2-
"files.associations": {
3-
"math.h": "c",
4-
"stdlib.h": "c"
5-
}
2+
"files.associations": {
3+
"math.h": "c",
4+
"stdlib.h": "c"
5+
},
6+
"C_Cpp_Runner.cCompilerPath": "/usr/bin/gcc",
7+
"C_Cpp_Runner.debuggerPath": "/usr/bin/gdb"
68
}

bfs.c

+69-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#define DEBUG if(1)
4+
#define DEBUG if(0)
55
#define MAX_SIZE 10000
66
//https://thehuxley.com/problem/805
77

@@ -110,16 +110,16 @@ void printGraph(graph* gr, int vertexNum)
110110
for (int i = 0; i < vertexNum; i++)
111111
{
112112
adj_list* tmp = gr->vertices[i];
113-
printf("v[%d]: ",i);
113+
printf("vertices[%d]: ",i);
114114
while (tmp)
115115
{
116-
printf("v(%d),",tmp->item);
116+
printf("v(%d), ",tmp->item);
117117
tmp= tmp->next;
118118
}
119119
printf("\n");
120120
}
121121
}
122-
void printQ(Queue* q)
122+
void printQ(Queue* q, graph* g)
123123
{
124124
adj_list* tmp = q->head;
125125
while (tmp != NULL)
@@ -129,73 +129,101 @@ void printQ(Queue* q)
129129
}
130130
printf("\n");
131131

132+
}
133+
void theWay(graph* g,int distToSource[], int lasts[],int origin, int destination, int pass)
134+
{
135+
int buffer[distToSource[destination]];
136+
//printf("[[%d]]\n",distToSource[destination]);
137+
int i = 0;
138+
while (pass != origin)
139+
{
140+
buffer[i] = lasts[pass];
141+
pass = lasts[pass];
142+
i++;
143+
}
144+
printf("Caminho entre %d e %d: ",origin,destination);
145+
for(int i = distToSource[destination]-1 ; i>= 0 ;i--)
146+
{
147+
printf("%d => ",buffer[i]);
148+
}
149+
printf("%d\n",destination);
150+
132151
}
133152
void bfs(graph* graph,int origin, int destination, int count)
134153
{
135154
for (int i = 0; i < MAX_SIZE; i++)
136155
{
137156
graph->visited[i] = 0;
138157
}
139-
158+
159+
int verticesNum = count;
140160
adj_list* tmp = graph->vertices[origin];
141161
Queue* queue = initQ();
142162
int dequeued;
143-
graph->visited[origin] = 1;
144-
//DEBUG printf("\tgraph->visited[%d] = %d\n",origin,graph->visited[origin]);
145163

164+
graph->visited[origin] = 1;
146165
enQueue(queue,origin);
147166
printf("Iniciando busca em largura a partir de %d\n",origin); //para a primeira entrada
148-
DEBUG printQ(queue);
149-
150-
int lasts[MAX_SIZE];
167+
int distToSource[MAX_SIZE];
168+
distToSource[origin] = 0;
169+
int lasts[MAX_SIZE]; //vai guardar qual o pai do index... se 3 é pai de 2, lasts[2] = 3
151170
int found = 0;
171+
count = 0;
152172
while (!isEmptyQ(queue))
153173
{
154-
//DEBUG printf("Fila não está vazia ainda.\n");
155-
dequeued = deQueue(queue);
174+
dequeued = deQueue(queue); //dequeued é o numero que vai ser visitado,
156175
tmp = graph->vertices[dequeued];
157-
while (tmp != NULL && !graph->visited[tmp->item])
176+
if(dequeued == destination) found = 1; //aqui vemos os vertices que ele tem adjacentes
177+
while (tmp != NULL)
158178
{
159-
//DEBUG printf("found == [%d], tmp = [%d]\n",found,tmp->item);
160-
//if(tmp->next != NULL) DEBUG printf("Visitando [%d], proximo = [%d]\n",tmp->item,tmp->next->item);
179+
if(!graph->visited[tmp->item])
180+
{
181+
graph->visited[tmp->item] = 1;
182+
enQueue(queue,tmp->item);
183+
lasts[tmp->item] = dequeued;
184+
distToSource[tmp->item] = distToSource[dequeued]+1;
185+
//DEBUG printQ(queue,graph);
186+
}
187+
else
188+
{
189+
tmp = tmp->next;
190+
continue;
191+
}
161192
printf("Iniciando busca em largura a partir de %d\n",tmp->item);
162193
if(found == 0){ lasts[count] = tmp->item; count++;}
163194
if(tmp->item == destination)
164195
{
165196
found = 1;
166-
//count++;
197+
//DEBUG printf("encontrado [%d]!", found);
167198
//DEBUG printf("ENCONTRADO depois de %d passagens, ultimo foi [%d]\n",count,lasts[count-1]);
168199
//break;
169200
}
170-
if(!graph->visited[tmp->item])
171-
{
172-
// DEBUG printf("\tgraph->visited[%d] = %d\n",tmp->item,graph->visited[tmp->item]);
173-
graph->visited[tmp->item] = 1;
174-
enQueue(queue,tmp->item);
175-
DEBUG printQ(queue);
176-
}
177201
tmp = tmp->next;
178-
count++;
179-
202+
//count++;
180203
}
181-
}
182204

205+
}
183206
//node_explanation(graph);
184-
185-
207+
printf("\n");
208+
for (int i = 0; i < verticesNum; i++)
209+
{
210+
switch (graph->visited[i])
211+
{
212+
case 0:
213+
printf("%d | - | -\n", i);
214+
break;
215+
default:
216+
if(distToSource[i] == 0) {printf("%d | %d | -\n", i,distToSource[i]); break;}
217+
printf("%d | %d | %d\n", i,distToSource[i],lasts[i]);
218+
break;
219+
}
220+
}
221+
printf("\n");
186222
if (found == 0) printf("Sem caminho entre %d e %d\n\n",origin,destination);
187223
else
188224
{
189-
printf("Caminho entre %d e %d: %d => ",origin,destination,origin);
190-
for (int i = 0; i < count; i++)
191-
{
192-
if (i == count-1)
193-
{
194-
printf("%d\n",lasts[i]);
195-
break;
196-
}
197-
printf("%d => ",lasts[i]);
198-
}
225+
theWay(graph,distToSource,lasts,origin,destination,destination);
226+
printf("\n");
199227
}
200228
DEBUG printf("FIM DA BUSCA!\n");
201229

@@ -214,26 +242,22 @@ int main()
214242
scanf("%d",&n2);
215243
addEdge(graph,n1,n2);
216244
}
217-
218-
//printGraph(graph,verticesNum);
219245
for (int i = 0; i < verticesNum; i++)
220246
{
221247
//DEBUG printf("indo para [%d]\n",i);
222248
bubbleSort(graph->vertices[i]);
223249
}
224-
printGraph(graph,verticesNum);
225-
226-
250+
DEBUG printGraph(graph,verticesNum);
227251
for (int i = 0; i < testNum; i++)
228252
{
229253
int n1,n2;
230254
scanf("%d",&n1); //origin
231255
scanf("%d",&n2); //destination
232256
printf("--------\n\nCaso de Teste #%d - BFS(%d)\n\n",i+1,n1);
233257
DEBUG printf("Alvo: [%d]\n",n2);
234-
bfs(graph,n1,n2,0);
258+
bfs(graph,n1,n2,verticesNum);
235259
}
236-
260+
printf("--------\n");
237261

238262
return 0;
239263
}

0 commit comments

Comments
 (0)