Educational Codeforces Round 5 C.The Labyrinth(dfs+并查集)

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 cellsadjacent if they share a side.

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.

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.

The answer should be printed as a matrix with n rows andm columns. Thej-th symbol of thei-th row should be "." if the cell is empty at the start. Otherwise thej-th symbol of the i-th row should contain the only digit —- the answer modulo10. The matrix should be printed without any spaces.

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

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to usescanf/printf instead ofcin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/System.out inJava.

Input

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

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

Output

Print the answer as a matrix as described above. See the examples to precise the format of the output.

Sample test(s)
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 to component of size5 (cross). If any of the corner cell will be empty then it will be included to component of size3 (corner).


思路就是通过一次遍历,把各个"."的所在连通块编号cnt确定,然后size[cnt]放的是各个连通块的大小。

我之前的做法是每次遍历到“*”,都进入DFS,很显然T了。

set就是用来去重的,我自己用unique函数试过,Wa了,我也不知道为什么=-=。

CF里交的时候选G+11可以用for(int k:q)来代替那一长串。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<set>
using namespace std;
char map[1005][1005];
int vis[1005][1005];
int n,m;
int cnt;
int size[1005*1005];
int direction[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
void dfs(int i,int j)
{
	if(i<0||i>=n||j<0||j>=m)
		return;
	if(vis[i][j]||map[i][j]=='*')
		return;
	vis[i][j]=cnt;
	size[cnt]++;
	for(int k=0;k<=3;k++)
	{
		int ni=i+direction[k][0];
		int nj=j+direction[k][1];
		dfs(ni,nj);
	}
}
int main(void)
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(vis,0,sizeof(vis));
		memset(size,0,sizeof(size));
		cnt=0;
		getchar();
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				scanf("%c",&map[i][j]);
			getchar();
		}
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(map[i][j]=='.'&&!vis[i][j])
				{
					cnt++;
					dfs(i,j);
				}
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if(map[i][j]=='*')
				{
					int t=1;
					set<int>q;
					for(int k=0;k<=3;k++)
					{
						int ni=i+direction[k][0];
		                int nj=j+direction[k][1];
						if(ni<0||ni>=n||nj<0||nj>=m)
							continue;
						if(map[ni][nj]=='.')
							q.insert(vis[ni][nj]);
					}
                    for(set<int>::iterator k=q.begin();k!=q.end();k++)
						t+=size[*k];
					map[i][j]=t%10+'0';
				}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				printf("%c",map[i][j]);
			printf("\n");
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值