1
1
#include <stdio.h>
2
2
#include <stdlib.h>
3
3
#include <string.h>
4
- #define DEBUG if(1 )
4
+ #define DEBUG if(0 )
5
5
#define MAX_SIZE 10000
6
6
//https://thehuxley.com/problem/805
7
7
@@ -110,16 +110,16 @@ void printGraph(graph* gr, int vertexNum)
110
110
for (int i = 0 ; i < vertexNum ; i ++ )
111
111
{
112
112
adj_list * tmp = gr -> vertices [i ];
113
- printf ("v [%d]: " ,i );
113
+ printf ("vertices [%d]: " ,i );
114
114
while (tmp )
115
115
{
116
- printf ("v(%d)," ,tmp -> item );
116
+ printf ("v(%d), " ,tmp -> item );
117
117
tmp = tmp -> next ;
118
118
}
119
119
printf ("\n" );
120
120
}
121
121
}
122
- void printQ (Queue * q )
122
+ void printQ (Queue * q , graph * g )
123
123
{
124
124
adj_list * tmp = q -> head ;
125
125
while (tmp != NULL )
@@ -129,73 +129,101 @@ void printQ(Queue* q)
129
129
}
130
130
printf ("\n" );
131
131
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
+
132
151
}
133
152
void bfs (graph * graph ,int origin , int destination , int count )
134
153
{
135
154
for (int i = 0 ; i < MAX_SIZE ; i ++ )
136
155
{
137
156
graph -> visited [i ] = 0 ;
138
157
}
139
-
158
+
159
+ int verticesNum = count ;
140
160
adj_list * tmp = graph -> vertices [origin ];
141
161
Queue * queue = initQ ();
142
162
int dequeued ;
143
- graph -> visited [origin ] = 1 ;
144
- //DEBUG printf("\tgraph->visited[%d] = %d\n",origin,graph->visited[origin]);
145
163
164
+ graph -> visited [origin ] = 1 ;
146
165
enQueue (queue ,origin );
147
166
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
151
170
int found = 0 ;
171
+ count = 0 ;
152
172
while (!isEmptyQ (queue ))
153
173
{
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,
156
175
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 )
158
178
{
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
+ }
161
192
printf ("Iniciando busca em largura a partir de %d\n" ,tmp -> item );
162
193
if (found == 0 ){ lasts [count ] = tmp -> item ; count ++ ;}
163
194
if (tmp -> item == destination )
164
195
{
165
196
found = 1 ;
166
- //count++ ;
197
+ //DEBUG printf("encontrado [%d]!", found) ;
167
198
//DEBUG printf("ENCONTRADO depois de %d passagens, ultimo foi [%d]\n",count,lasts[count-1]);
168
199
//break;
169
200
}
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
- }
177
201
tmp = tmp -> next ;
178
- count ++ ;
179
-
202
+ //count++;
180
203
}
181
- }
182
204
205
+ }
183
206
//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" );
186
222
if (found == 0 ) printf ("Sem caminho entre %d e %d\n\n" ,origin ,destination );
187
223
else
188
224
{
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" );
199
227
}
200
228
DEBUG printf ("FIM DA BUSCA!\n" );
201
229
@@ -214,26 +242,22 @@ int main()
214
242
scanf ("%d" ,& n2 );
215
243
addEdge (graph ,n1 ,n2 );
216
244
}
217
-
218
- //printGraph(graph,verticesNum);
219
245
for (int i = 0 ; i < verticesNum ; i ++ )
220
246
{
221
247
//DEBUG printf("indo para [%d]\n",i);
222
248
bubbleSort (graph -> vertices [i ]);
223
249
}
224
- printGraph (graph ,verticesNum );
225
-
226
-
250
+ DEBUG printGraph (graph ,verticesNum );
227
251
for (int i = 0 ; i < testNum ; i ++ )
228
252
{
229
253
int n1 ,n2 ;
230
254
scanf ("%d" ,& n1 ); //origin
231
255
scanf ("%d" ,& n2 ); //destination
232
256
printf ("--------\n\nCaso de Teste #%d - BFS(%d)\n\n" ,i + 1 ,n1 );
233
257
DEBUG printf ("Alvo: [%d]\n" ,n2 );
234
- bfs (graph ,n1 ,n2 ,0 );
258
+ bfs (graph ,n1 ,n2 ,verticesNum );
235
259
}
236
-
260
+ printf ( "--------\n" );
237
261
238
262
return 0 ;
239
263
}
0 commit comments