CodeForces 616C The Labyrinth (二维并查集)

来源:互联网 发布:qt编程用什么软件 编辑:程序博客网 时间:2024/05/21 06:28

题目链接:http://codeforces.com/problemset/problem/616/C点击打开链接

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 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. The j-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 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 ofn 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 usescanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/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 size5 (cross). If any of the corner cell will be empty then it will be included to component of size3 (corner).

因为需要寻找连通图 因此第一反应是并查集 将能够连通的部分归在一类

另一种简便做法是标号广搜 这里就只贴并查集的代码

#include<bits/stdc++.h>#define maxn 1000010using namespace std;struct xjy{    int x;    int y;};int dir[4][2]={1,0,0,1,-1,0,0,-1};char mmap[1111][1111];xjy pre[1111][1111];int num[1111][1111];xjy findx(xjy xx){    xjy r=xx;    while(pre[r.x][r.y].x!=r.x||pre[r.x][r.y].y!=r.y)        {            r=pre[r.x][r.y];        }    xjy i=xx,j;    while(pre[i.x][i.y].x!=r.x||pre[i.x][i.y].y!=r.y)    {        j=pre[i.x][i.y];        pre[i.x][i.y]=r;        i=j;    }    return r;}void join (xjy xx,xjy yy){    xjy p1=findx(xx);    xjy p2=findx(yy);    if(p1.x!=p2.x||p1.y!=p2.y)        {            pre[p1.x][p1.y]=p2;            num[p2.x][p2.y]+=num[p1.x][p1.y];            num[p1.x][p1.y]=0;        }}int main(){    for(int i=0;i<=1001;i++)        for(int j=0;j<=1001;j++)    {        xjy mid;        mid.x=i;        mid.y=j;        pre[i][j]=mid;        num[i][j]=0;        mmap[i][j]='*';    }    int n,m;    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            char c;            scanf(" %c",&c);            if(c=='.')                {                    mmap[i][j]=c;                    num[i][j]=1;                }            else                mmap[i][j]=c;        }     for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            if(mmap[i][j]=='.')           {                xjy mid;                mid.x=i;                mid.y=j;                xjy midmid;                for(int ii=0;ii<4;ii++)                {                    midmid.x=mid.x+dir[ii][0];                    midmid.y=mid.y+dir[ii][1];                    if(mmap[midmid.x][midmid.y]=='.')                    {                        join(mid,midmid);                    }                }            }        }    for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)        {            if(mmap[i][j]=='*')            {                int sum=0;                xjy mid;                mid.x=i;                mid.y=j;                vector<xjy > sss;                for(int ii=0;ii<4;ii++)                {                    xjy midmid;                    midmid.x=mid.x+dir[ii][0];                    midmid.y=mid.y+dir[ii][1];                    if(mmap[midmid.x][midmid.y]=='.')                    {                        sum=0;                        midmid=findx(midmid);                        //printf("%d %d\n",midmid.x,midmid.y);                        //printf("%d\n",sss.size());                        if(sss.size()==0)                            sss.push_back(midmid);                        else                        {                            int eend=sss.size();                            int flag=1;                            for(int it=0;it!=eend;it++)                                if(midmid.x==sss[it].x&&midmid.y==sss[it].y)                                    {                                        flag=0;                                    }                            if(flag)                                sss.push_back(midmid);                        }                    }                }                for(int it=0;it!=sss.size();it++)                {                    xjy midmidmid=findx(sss[it]);                    int midx=midmidmid.x;                    int midy=midmidmid.y;                    sum+=num[midx][midy];                }                printf("%d",(sum+1)%10);            }            else                printf("%c",mmap[i][j]);        }        printf("\n");        }}





原创粉丝点击