CodeForces - 616C The Labyrinth(BFS+预处理)

CodeForces - 616C

http://codeforces.com/problemset/problem/616/C

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

题意:给定一张m*n的图,相连的“.”组成连通块。求如果将且仅将任意位置的“*”换成“.”后,包含此格的新的连通块数量ans(对10取模),打印将“*”替换成(ans%10)的图。

分析:m,n数量级是1e3,如果暴力将出现的“*”换成“.”每个BFS一次,那想必是会超时的。所以首先想到预处理。

思考之后发现:每个“*”位置的值等于上下左右四个格子的连通块大小之和 + 1。如果上下左右格子是同一个连通块就只算一次,否则就重复了。想好这个开始预处理。

过程是先求出图中X个连通块的大小,vst数组用大小不同的数字1-X标记已访问,相当于给每个连通块唯一的标号。然后用map记录每个标号x对应连通块大小mm[x]。最后遍历一次图输出。如果位置是“*”就把标号不同的上下左右格子的连通块大小加上。这一步可以用set去重。注意每个结果模10。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define ll long long
int dir[4][2]={0,1,0,-1,1,0,-1,0};
ll n,m;
char maps[1005][1005];
int vst[1005][1005];
int num[1005][1005];
map<ll,ll> mm;
set<ll> ss;
struct Point{
    ll x,y;
};

int check(Point a){
    if(a.x>=0 && a.x<n && a.y>=0 && a.y<m && maps[a.x][a.y]!='*' && !vst[a.x][a.y])return 1;
    return 0;
}

int check2(Point a){
    if(a.x>=0 && a.x<n && a.y>=0 && a.y<m && maps[a.x][a.y]!='*')return 1;
    return 0;
}

void bfs(){
    memset(vst,0,sizeof(vst));
    memset(num,0,sizeof(num));
    int cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            Point x,y;
            x.x = i;
            x.y = j;
            if(check(x)){
                cnt++;
                queue<Point> q;
                vst[x.x][x.y]=cnt;
                q.push(x);
                int count=1;
                while(!q.empty()){
                    x=q.front();
                    q.pop();
                    for(int k=0;k<4;k++){
                        y.x = x.x + dir[k][0];
                        y.y = x.y + dir[k][1];
                        if(check(y)){
                            vst[y.x][y.y]=cnt;
                            q.push(y);
                            count++;
                        }
                    }
                }
                mm[cnt]=count;
            }
        }
    }
}

int main(){
    cin >> n >> m;
    for(int i=0;i<n;i++){
        cin >> maps[i];
    }
    bfs();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(maps[i][j]=='*'){
                int ans=0;
                ss.clear();
                for(int k=0;k<4;k++){
                    Point p;
                    p.x = i+dir[k][0];
                    p.y = j+dir[k][1];
                    if(check2(p)){
                        ss.insert(vst[p.x][p.y]);
                    }
                }
                for(set<ll>::iterator k=ss.begin();k!=ss.end();k++){
                    ans+=mm[*k];
                }
                cout << (ans+1)%10;
            }
            else cout << '.';
        }
        cout << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值