BFS(未完)

代码未调试完成见p277

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

struct node{
	int x,y,z;
};
int m,n;
queue<node> q;
void BFS(vector<bool> *s,vector<node>*v,int xx,int yy);
int main(){
int ans=0;

scanf("%d%d",&m,&n);
vector<bool>s[m];
vector<node>v[m];

for(int i=0;i<n;i++){
	for(int j=0;j<m;j++){
	cin>>v[i][j].z;
	v[i][j].x=i;v[i][j].y=j;
	}
}
	
for(int i=0;i<n;i++)
	for(int j=0;j<m;j++){
		if(v[i][j].z!=0&&!s[m][n]){
			ans++;
			BFS(s,v,i,j);
		}
		
	}
	cout<<"ans="<<ans;
	return 0;
} 


void BFS(vector<bool> *s,vector<node> *v,int xx,int yy){
	if(xx<0||yy<0||xx>=m||yy>=n) return;
	if(v[xx][yy].z==1){
		q.push(v[xx][yy]);
		s[xx][yy]=true;
	}
	while(!q.empty()){
		for(int i=-1;i<2;i++)
			for(int j=-1;j<2;j++){
			BFS(s,v,q.front().x+i,q.front().y+j);
			}
			q.pop();
	}
	
}
//6 7
//0 1 1 1 0 0 1
//0 0 1 0 0 0 0
//0 0 0 0 1 0 0
//0 0 0 1 1 1 0
//1 1 1 0 1 0 0
//1 1 1 1 0 0 0


猜你喜欢

转载自blog.csdn.net/Cai__ji/article/details/85012013
BFS