The Labyrinth

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 <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int N=1010;

int n,m;
char s[N][N];
int vis[N][N],cnt[N*N];
int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

struct node 
{
    int x;
    int y;
}head,tail;

int bfs(int x,int y,int num)
{
    queue<node>q;
    head.x=x;
    head.y=y;
    int ans=0;
    q.push(head);

    while(!q.empty())
    {
        head=q.front();
        q.pop();

        ans++;
        for(int i=0;i<4;i++)
        {
            tail.x=head.x+step[i][0];
            tail.y=head.y+step[i][1];
            if(tail.x>=1&&tail.x<=n&&tail.y>=1&&tail.y<=m&&s[tail.x][tail.y]=='.'&&!vis[tail.x][tail.y])
            {
                vis[tail.x][tail.y]=num;
                q.push(tail);
            }
        }
    }
    return ans;
}

int main()
{
    while(cin>>n>>m)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)   cin>>s[i]+1;

        int num=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='.'&&!vis[i][j])
                {
                    vis[i][j]=num;
                    cnt[num]=bfs(i,j,num);
                    num++;
                }
            }
        }

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]=='.')    cout<<'.';
                else
                {
                    int ans=cnt[vis[i-1][j]]+1;
                    if(vis[i-1][j]!=vis[i+1][j])
                        ans+=cnt[vis[i+1][j]];
                    if(vis[i][j-1]!=vis[i-1][j]&&vis[i][j-1]!=vis[i+1][j])
                        ans+=cnt[vis[i][j-1]];
                    if(vis[i][j+1]!=vis[i][j-1]&&vis[i][j+1]!=vis[i-1][j]&&vis[i][j+1]!=vis[i+1][j])
                        ans+=cnt[vis[i][j+1]];
                    cout<<ans%10;
                } 
            }
            cout<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值