The Labyrinth (dfs)

H - The Labyrinth
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

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 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.

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.

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

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 Input

Input
3 3
*.*
.*.
*.*
Output
3.3
.5.
3.3
Input
4 5
**..*
..***
.*.*.
*.*.*
Output
46..3
..732
.6.4.
5.4.3

Hint

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


题意:点代表联通的,星号代表不联通的,每次把一个星号变成点号,看联通块的联通分支的数量,每次只改变当前的星号,当遍历到其他星号时,已经遍历到过的星号还看成是星号。

思路:刚开始我用dfs直接搜每个星号旁边的点号数目,果然TLE。为了不超时,就把每个联通块标记成一个编号,然后每个编号代表着联通块的联通分支的数量,每个星号旁边所有的点号代表的不同联通块的和加上1(*号本身),就是改点的数字大小。


#include<cstring>
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
char map[1005][1005];
int go[4][2]={0,1,0,-1,1,0,-1,0};//方向 
int cnt=0;//联通块种类计数器,每个数字代表一种联通块 
int n,m;
int size[1000005];//size【cnt】代表第cnt种联通块含有.的数量 
int vis[1005][1005];//标记联通块 
void dfs(int i,int j){
	int k;
	vis[i][j]=cnt;
	size[cnt]++;//第cnt种的联通块的联通分支数量加1 
	for(k=0;k<4;k++){
		if(map[i+go[k][0]][j+go[k][1]]=='.'&&i+go[k][0]>=0&&i+go[k][0]<n&&j+go[k][1]>=0&&j+go[k][1]<m&&vis[i+go[k][0]][j+go[k][1]]==0)
		dfs(i+go[k][0],j+go[k][1]);
	}
	
}
int main(){
    int i,j,t;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++){
    	scanf("%s",map[i]);
    }
    
    for(i=0;i<n;i++)
     for(j=0;j<m;j++){
     	if(map[i][j]=='.'&&!vis[i][j]){
     		cnt++;
		 dfs(i,j);
		 }//通过深搜给每个联通块编号 
     }
     for(i=0;i<n;i++)
      for(j=0;j<m;j++){
      	if(map[i][j]=='*'){//遍历每个星号 
	      	int cnt2=1;
	      	set<int>s;//用set是为了防止四个方向有联通块重复 
	      	for(t=0;t<4;t++){//星号的每个方向的联通块 
	      		if(map[i+go[t][0]][j+go[t][1]]=='.'&&i+go[t][0]>=0&&i+go[t][0]<n&&j+go[t][1]>=0&&j+go[t][1]<m)
	      		{s.insert(vis[i+go[t][0]][j+go[t][1]]);
			  }
	      	}
	      	for(set<int>::iterator it=s.begin();it!=s.end();it++)
	      	cnt2+=size[*it]; 
	      	map[i][j]=cnt2%10+'0';
	      }
      }

    for(i=0;i<n;i++)
	puts(map[i]); 
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值