#include<stdio.h>
#define maxx 101
int Q[maxx];
int first ,last;
int node, edge;
char linker[maxx][maxx];
char var[maxx];
int path[maxx];
void Ini() {
int i, j;
for(i = 1; i<= node; i++) {
for(j = 1; j<= node; j++) {
linker[i][j] = 0;
}
path[i] = i;
var[i] = 0;
}
}
//Inserts element in the Q ie ENQUEUE
void Insert(int n) {
Q[last++] = n;
last %= maxx;
}
int Pop() {
int n = Q[first++];
first %= maxx;
return n;
}
int isEmpty() {
if(first == last) return 0;
return 1;
}
void Printpath(int n) {
if(n == path[n]) {
printf("%d",n);
return;
}
Printpath(path[n]);
printf(" %d",n);
}
int BFS(int s, int ter) {
int u, v, i;
Insert(s);
var[s] = 1;
while(isEmpty() == 1) {
v = Pop();
if(v == ter) {
Printpath(v);
return 1;
}
for(i = 1; i<= node; i++) {
if(linker[v][i] == 1 && var[i] == 0) {
var[i] = 1;
Insert(i);
path[i] = v;
}
}
}
return -1;
}
void main() {
int i, u, v;
scanf("%d%d",&node,&edge);
Ini();
while(edge--) {
scanf("%d%d",&u,&v);
linker[u][v] = linker[v][u] = 1;
}
scanf("%d%d",&u,&v);
BFS(u,v);
printf("\n");
}
Edited by WingedPanther, 01 January 2011 - 09:21 AM.
delete spammy links, add code tags


Sign In
Create Account

Back to top









