您的位置:首页 > 其它

图结构练习——BFS——从起始点到目标点的最短步数(bfs)

2014-02-19 12:02 971 查看
题目描述不敢发了,怕又说我打广告。。。

#include <stdio.h>
#include <stdlib.h>
struct node
{
int x,c;
}q[10005];
int map[1005][1005];
int v[10005];
void bfs(int x)
{
int i,e=1,s=0;
struct node t,f;
t.x=x;
t.c=0;
q[0]=t;
while(s<e)
{
t=q[s++];
if(map[t.x][1]==1)
{
printf("%d\n",t.c+1);
return ;
}
for(i=x-1;i>=1;i--)
{
f.x=i;
if(!v[i]&&map[t.x][i]==1)
{
f.c=t.c+1;
q[e++]=f;
v[f.x]=1;
}
}

}
printf("NO\n");
}
int main()
{
struct node *p;
int n,m,a,b;
while(~scanf("%d%d",&n,&m))
{
memset(v,0,sizeof(v));
memset(map,0,sizeof(map));
while(m--)
{
scanf("%d%d",&a,&b);
map[a][b]=1;
}
v
=1;
bfs(n);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐