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

这篇博客详细介绍了如何解决CodeForces上的616C问题——The Labyrinth。文章通过一个n×m的矩阵表示迷宫,讨论了空地与障碍物相邻的概念,并定义了连通分量。任务是计算当每个障碍物变成空地时,其所属的最大连通区域的大小。博客提供了输入输出的建议和样例,并且提到了两种解题方法:一种是并查集,另一种是标记广搜。博主主要分享了并查集的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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 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 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).

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

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

#include<bits/stdc++.h>
#define maxn 1000010
using 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");
        }

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值