codeforces_616C. The Labyrinth

来源:互联网 发布:java的数组定义 编辑:程序博客网 时间:2024/05/01 09:16
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.

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


很少做dfs的题,一直找不到爆栈的原因,原来是忘了标记了,傻。
先保存每个连通块的'.'数量,然后再从每个'*'四周的相邻连通块算出总连通块的'.'数量,注意不要重复计算。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;int diri[4]= {-1, 0, 1, 0};int dirj[4]= { 0,-1, 0, 1};char a[1010][1010];char ans[1010][1010];int mark[1010][1010];int num=0,res=0;int count[1000010];int t[4];int dfs(int i,int j){    num++;    ans[i][j]='.';a[i][j]='#';    mark[i][j]=res;    for(int k=0;k<4;k++)    {        int ii=i+diri[k];        int jj=j+dirj[k];        if(a[ii][jj]!='.')continue;        dfs(ii,jj);    }}int main(){    int i,j,n,m;    scanf("%d%d",&n,&m);    char cc=getchar();    memset(a,'\0',sizeof(a));    for(i=1; i<=n; i++)    {        for(j=1; j<=m; j++)        {            scanf("%c",&a[i][j]);        }        char ch=getchar();    }    for(i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            if(a[i][j]=='.')            {                res++;num=0;                dfs(i,j);                count[res]=num;            }        }    }    for(i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            if(a[i][j]=='*')            {                int nn=1;                for(int k=0;k<4;k++)                    t[k]=count[mark[i+diri[k]][j+dirj[k]]];                for(int k=0;k<4;k++)                {                    nn+=count[mark[i+diri[k]][j+dirj[k]]];                    count[mark[i+diri[k]][j+dirj[k]]]=0;                }                for(int k=0;k<4;k++)                count[mark[i+diri[k]][j+dirj[k]]]=t[k];                ans[i][j]=nn%10+'0';            }        }    }    for(i=1;i<=n;i++)printf("%s\n",ans[i]+1);    return 0;}




0 0
原创粉丝点击