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;
}

 

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.youkuaiyun.com/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.youkuaiyun.com/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值