-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.c
37 lines (35 loc) · 836 Bytes
/
BFS.c
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
#include<stdio.h>
int vertex,edge,end=0,front=0;
int map[101][101];
int ans[101],numofedge[101];
int que[1001];
int main(void){
scanf("%d %d",&vertex,&edge);
int a,b;
for(int i=0;i<edge;i++){
scanf("%d %d",&a,&b);
map[a][numofedge[a]]=b;
numofedge[a]++;
map[b][numofedge[b]]=a;
numofedge[b]++;
}
int s,f;
scanf("%d %d",&s,&f);
que[end]=s;
end+=1;
ans[s]=1;
while(front!=end){
int now=que[front];
for(int i=0;i<numofedge[now];i++){
int next=map[now][i];
if(ans[next]==0){
ans[next]=ans[now]+1;
que[end]=next;
end++;
}
}
front++;
}
if(ans[f]==0)puts("Oops");
else printf("%d\n",ans[f]-1);
}