赞
踩
伪代码
- #define Black 2
- #define Gray 1
- #define White 0
- #define Infinite -1
- #define NIL -1
-
- struct map{
- int d;
- int color;
- int parent;
- } vertix[100];
-
- void BFS(int a[100][100],int n,int s){
- int i,j;
- int tmp;
- Queue q;
- for(i = 0;i<n;i++){
- vertix[i].d = Infinite;
- vertix[i].color = White;
- vertix[i].parent = NIL;
- }
- vertix[s].color = Gray;
- vertix[s].d = 0;
- q = CreateQueue(n);
- Enqueue(s,q);
- while(!IsEmpty(q)){
- tmp = FrontAndDequeue(q);
- for(i = 0;i<n;i++)
- if(a[tmp][i])
- if(vertix[i].color == White){
- vertix[i].d = vertix[tmp].d + 1;
- vertix[i].color = Gray;
- vertix[i].parent = tmp;
- Enqueue(i,q);
- }
- vertix[tmp].color = Black;
- }
- DisposeQueue(q);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。