The Labyrinth bfs

You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.node

Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.ios

For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.git

The answer should be printed as a matrix with nrows and m columns. The j-th symbol of the i-th row should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit —- the answer modulo 10. The matrix should be printed without any spaces.ide

To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.ui

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.spa

 

 

题目大意:求每个obstacle打通后联通的数量code

 

 

对全部的可走位置进行一次bfs 用染色标记(allcnt) 求得每一个可行位置的数存在num里面。而后对于每个*找他上下左右的联通数量加起来,用set判重,代码长的一比component

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<set> 
using namespace std;

int n,m;
char gap[1080][1080];
int visit[1080][1080];
int num[1080000];
int dir[4][2]={-1,0,0,-1,1,0,0,1};
struct node
{
	int x,y;
};
int bfs(int x,int y,int allcnt)
{
	//	cout<<"sb";
	queue<node> q;
	int ans=1;
	node st,in,ne;
	st.x=x,st.y=y;
	q.push(st);
	visit[x][y]=allcnt;
	while(!q.empty())
	{
		in=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			ne.x=in.x+dir[i][0];
			ne.y=in.y+dir[i][1];
			if(gap[ne.x][ne.y]=='.'&&visit[ne.x][ne.y]==0&&ne.x>=1&&ne.x<=n&&ne.y>=1&&ne.y<=m)
			{
				visit[ne.x][ne.y]=allcnt;
				q.push(ne);
				ans++;
			}
		}
	}
	return ans;
	//cout<<"sb";
}



int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",gap[i]+1);
	}
	int allcnt=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(visit[i][j]==0&&gap[i][j]=='.')
			{
				int ans=bfs(i,j,allcnt);
				num[allcnt]=ans;
				allcnt++;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			int ans=0;
			set<int> s;
			if(gap[i][j]=='*')
			{
				if(i-1>=1&&visit[i-1][j]>0&&!s.count(visit[i-1][j]))
				{
					ans+=num[visit[i-1][j]];
					s.insert(visit[i-1][j]);
				}
				if(i+1<=n&&visit[i+1][j]&&!s.count(visit[i+1][j]))
				{
					ans+=num[visit[i+1][j]];
					s.insert(visit[i+1][j]);
				}
				if(j-1>=1&&visit[i][j-1]&&!s.count(visit[i][j-1]))
				{
					ans+=num[visit[i][j-1]];
					s.insert(visit[i][j-1]);
				}
				if(j+1<=m&&visit[i][j+1]&&!s.count(visit[i][j+1]))
				{
					ans+=num[visit[i][j+1]];
					s.insert(visit[i][j+1]);
				}
				ans++;
				ans%=10;
				gap[i][j]=(ans+'0'); 
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<gap[i][j];
		}
		cout<<endl;
	}
	//for(int i=1;i<=4;i++) cout<<num[i];
	return 0;
}