BFS模版程序

本文转自http://so.csdn.net/so/search/s.do?q=bfs&u=cnyali&t=blog

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
	int x,y,s;
};
struct node a[10000+10];
int map[101][101],p[101][101],d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main(){
	int i,j,k,m,n;
	int f,l,u,v;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			scanf("%d",&map[i][j]);
	f=0;l=1;
	a[1].x=1;a[1].y=1;a[1].s=0;
	p[1][1]=1;
	while(f<=l){
		f++;
		for(i=0;i<=3;i++){
			u=a[f].x+d[i][0];
			v=a[f].y+d[i][1];
			if(map[u][v] && !p[u][v]){
				a[++l].x=u;
				a[l].y=v;
				a[l].s=a[f].s+1;
				p[u][v]=1;
				if(map[u][v]==2){
					printf("(%d,%d)->%d
",u,v,a[l].s);
				}
			}
		}
	}
	system("pause");
	return 0;
}
/*
10 10
1 1 1 0 1 1 1 1 1 1
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 1 1
1 0 1 0 2 0 2 0 1 1
1 0 0 0 0 0 0 0 1 1
1 0 1 0 1 0 1 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 0 2 1 1 1
1 1 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 1 1
*/



原文地址:https://www.cnblogs.com/llguanli/p/7109974.html