Codeforces - 723D. Lakes in Berland,bfs+sort

题目:

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

Let's calla connected component any non-extendible set of cells such that anytwo of them are connected by the path of adjacent cells. It is a typicalwell-known definition of a connected component.

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

The answer shouldbe printed as a matrix with n rows 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 ofthe i-th row should contain the only digit —- the answermodulo 10. The matrix should be printed without any spaces.

To make youroutput faster it is recommended to build the output as an array of n stringshaving length m and print it as a sequence of lines. It willbe much faster than writing character-by-character.

As input/outputcan reach huge size it is recommended to use fast input/output methods: forexample, prefer to use scanf/printf instead of cin/cout inC++, prefer to use BufferedReader/PrintWriter insteadof Scanner/System.out in Java.

Input

The first linecontains two integers n, m (1 ≤ n, m ≤ 1000)— the number of rows and columns in the field.

Each of thenext n lines contains m symbols:"." for empty cells, "*" for impassable cells.

Output

Print the answeras a matrix as described above. See the examples to precise the format of theoutput.

Example

Input

3 3
*.*
.*.
*.*

Output

3.3
.5.
3.3

Input

4 5
**..*
..***
.*.*.
*.*.*

Output

46..3
..732
.6.4.
5.4.3

Note

In first example,if we imagine that the central cell is empty then it will be included tocomponent of size 5 (cross). If any of the corner cell will be emptythen it will be included to component of size 3 (corner).

思路:

题意求每个‘*’上下左右相连的'.'的个数包括‘*’本身。

不可以对每个‘*’直接进行bfs或者dfs 会t

预处理出每个联通的‘.'的区域,编号,并且记录个数。再扫一遍判断上下左右的‘.'的个数即可

代码:

#include<cstdio>
#include<queue>
#include<set>
#include<cstring>
using namespace std;
const int N=1005;
char map[N][N];char out[N][N];
int vis[N][N];
int sum[N];

struct point{
	int x,y;
	point(int a,int b):x(a),y(b){}
};
int dir[4][2]={-1,0,0,1,0,-1,1,0};
int pos=1;

void bfs(int x,int y){
	queue<point>que;
	que.push(point(x,y));
	vis[x][y]=pos;
	int num=1;
	while(!que.empty())
	{
		int tx=que.front().x,ty=que.front().y;
		que.pop();
		for(int i=0;i<4;i++){
			int dx=tx+dir[i][0],dy=ty+dir[i][1];
			if(!vis[dx][dy]&&map[dx][dy]=='.'){
				vis[dx][dy]=pos;
				que.push(point(dx,dy));
				num++;
			}
		}
	}
	sum[pos]=num;
	pos++;

}

void init(){
	pos=1;
	memset(map,'#',sizeof(map));
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
}

int main()
{
	int n,m;
	init();
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf(" %s",map[i]+1);
		out[i][m+1]='\0';
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(map[i][j]=='.'&&!vis[i][j])
				bfs(i,j);
		}
	}
	set<int>st;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(map[i][j]=='.') out[i][j]='.';
			else{
				int t=0;
				for(int k=0;k<4;k++){
					int dx=i+dir[k][0],dy=j+dir[k][1];
					if(map[dx][dy]=='.'&&!st.count(vis[dx][dy]))
						t+=sum[vis[dx][dy]],st.insert(vis[dx][dy]);
				}
				st.clear();
				out[i][j]=(t+1)%10+'0';
			}
		}
	}
	for(int i=1;i<=n;i++)
		printf("%s\n",out[i]+1);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值