Codeforces616C The Labyrinth

C. The Labyrinth
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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/printfinstead of cin/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 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 size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).



思路:

題意求每個‘*’上下左右相連的'.'的個數包括‘*’本身。

不可以對每個‘*’直接進行bfs或者dfs 會tle

預處理出每個聯通的‘.'的區域,編號,並且記錄個數。再掃一遍判斷上下左右的‘.'的個數即可


代碼:

#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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章