(CodeForces - 616C)The Labyrinth

(CodeForces - 616C)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).

题目大意:给出n*m的矩形图,问每一个’*’号周围有多少个’.’号(将‘*’变成‘.’号后一共有多少个’.’号),上下左右连同都算,最后将答案模10输出。

思路:显然对于每一个*号都去dfs会超时。这个时候我们可以预处理出每一个’.’号处于哪一个集合(vis[x][y]=mark),每一个集合有多少个’.’号(cnt),然后对于每一个’*’号我们就可以加上刚才预处理出来的上下左右的集合的’.’号,当然相同集合就不用重复计算。

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

typedef pair<int,int> pai;
const int maxn=1005;
const int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char a[maxn][maxn];
int vis[maxn][maxn],num[maxn][maxn];
int n,m; 
int cnt,mark;

vector<pai> ve;

bool check(int x,int y)
{
    return (x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&a[x][y]!='*');
}

void dfs(int x,int y)
{
    vis[x][y]=mark;
    ve.push_back(pai(x,y));
    for(int i=0;i<4;i++)
    {
        int tmpx=x+dir[i][0];
        int tmpy=y+dir[i][1];
        if(check(tmpx,tmpy))
        {
            cnt++;
            ve.push_back(pai(tmpx,tmpy));
            vis[tmpx][tmpy]=mark;
            dfs(tmpx,tmpy);
        }
    } 
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++) scanf("%s",a[i]);
        mark=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(a[i][j]=='.'&&!vis[i][j])
                {
                    cnt=1;
                    mark++;
                    dfs(i,j);
                    for(int k=0;k<ve.size();k++)
                        num[ve[k].first][ve[k].second]=cnt;
                    ve.clear();
                    //printf("%d %d %d\n",i,j,num[i][j]);
                }
        int ans;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='*')
                {
                    ans=1;  
                    if(i-1>=0&&a[i-1][j]=='.') ans+=num[i-1][j];
                    if(i+1<n&&a[i+1][j]=='.'&&vis[i+1][j]!=vis[i-1][j]) ans+=num[i+1][j];
                    if(j-1>=0&&a[i][j-1]=='.'&&vis[i][j-1]!=vis[i-1][j]&&vis[i][j-1]!=vis[i+1][j])
                        ans+=num[i][j-1];
                    if(j+1<m&&a[i][j+1]=='.'&&vis[i][j+1]!=vis[i-1][j]&&vis[i][j+1]!=vis[i+1][j]&&vis[i][j+1]!=vis[i][j-1])
                        ans+=num[i][j+1];
                    printf("%d",ans%10);
                }
                else printf(".");
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值