nyoj-92

#include<iostream>
#include<queue>
#include<string.h>
#include<fstream>
using namespace std;
struct point
{
	int x,y;
};
int w,h,map[970][1450];
int x[4]={-1,0,1,0 };
int y[4]={0 ,1,0,-1};
void bfs(int a,int b)
{
	queue<point>q;
	point t1,t2;
	t1.x=a;
	t1.y=b;
	q.push(t1);
	while(!q.empty())
	{
		t1=q.front();
		q.pop();
		for(int i=0;i<4;++i)
		{
			t2.x=t1.x+x[i];
			t2.y=t1.y+y[i];
			if(t2.x<0 || t2.x>h+1 || t2.y<0 || t2.y>w+1 || map[t2.x][t2.y]==0)//这个好像可以提高速度
	            continue;
			map[t2.x][t2.y]=0;
			q.push(t2);
		}
	}
}
int main()
{
	int t,i,j;
//	fstream cin("d:\\test.txt");
	cin>>t;
	while(t--)
	{
		cin>>w>>h;
		memset(map,1,sizeof(map));
		for(i=1;i<=h;++i)
			for(j=1;j<=w;++j)
				cin>>map[i][j];
		bfs(0,0);
		for(i=1;i<=h;++i)
			for(j=1;j<=w;++j)
			{
				if(j==w)
					cout<<map[i][j]<<endl;
				else
					cout<<map[i][j]<<" ";
			}
	}
	return 0;
}
		

  

你可能感兴趣的:(nyoj-92)