codeforces 616C The Labyrinth(flood fill)

C. 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/printfinstead 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.

Sample test(s)

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<cstdio>
#include<cstring>
#include<stack>
#include<iterator>
#include<queue>
#include<set>
#include<vector>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF=0x3f3f3f3f;
char G[1005][1005];
int mark[1005][1005];
int cnt[1005*1005];
int d[4][2]={1,0,0,1,-1,0,0,-1};
int n,m;
set<int>S;
void dfs(int x,int y,int id)
{
    mark[x][y]=id;
    cnt[id]++;
    for(int i=0;i<4;i++)
    {
        int xx=x+d[i][0];
        int yy=y+d[i][1];
        if(!mark[xx][yy]&&G[xx][yy]!=‘*‘&&xx>=0&&xx<n&&yy>=0&&yy<m)
        dfs(xx,yy,id);
    }
}
int main()
{
    int id=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",G[i]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(G[i][j]==‘.‘&&!mark[i][j])
                dfs(i,j,++id);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            if(G[i][j]==‘*‘)
            {
                int ans=0;
                for(int k=0;k<4;k++)
                {
                    int x=i+d[k][0];
                    int y=j+d[k][1];
                    S.insert(mark[x][y]);
                }
                for(set<int>::iterator iter=S.begin();iter!=S.end();iter++)
                    ans+=cnt[*iter];
                S.clear();
                ans++,ans%=10;
                printf("%d",ans);
            }
            else
                printf(".");
        puts("");
    }
    return 0;
}
时间: 2025-01-03 16:12:12

codeforces 616C The Labyrinth(flood fill)的相关文章

CodeForces 616C The Labyrinth

先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int maxn=1000+10; char s[maxn][maxn]; int Map[maxn][maxn]; int n,m; int dir[4][2]; int Belong[maxn*maxn]

Codeforces Round #538 (Div. 2) D. Flood Fill 【区间dp || LPS (最长回文序列)】

任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f

CodeForces - 1114D Flood Fill (区间dp)

You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ii-th square initially has the color cici. Let's say, that two squares ii and jj belong to the same connected component if ci=cjci=cj, and ci=ckci=ck 

洪水填充(Flood fill)算法

从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法. 因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域. 洪水填充算法接受三个参数:起始节点,目标节点特征和针对提取对象要执行的处理. 目前有许多实现方式,基本上都显式的或隐式的使用了队列或者栈. 洪水填充算法实现最常见有四邻域

USACO The Castle(flood fill)

题目请点我 题解: 这道题真的好蛋疼啊,首先题意不好理解,搞了半天复杂的要死,有那么多要求,还要求那么多东西,做到一半都不想做了...感觉没什么技术含量,还做起来死费劲儿.但是强迫症非得按顺序做题啊,最后还是一点点把它给调出来了,说什么flood fill,其实也就是那么回事,没什么算法上的技巧,就是见招拆招的感觉... 题意搞懂再做题,题意,不谢! 第一步:根据他的规则把房间画出来,遍历一遍把每个节点四面的墙给补上: 第二步:深搜: 目的1:记录深搜的次数,房间数: 目的2:记录深搜的深度,最

SSOJ 2316 面积【DFS/Flood Fill】

题目描述 编程计算由“1”号围成的下列图形的面积.面积计算方法是统计1号所围成的闭合曲线中点的数目. 如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15. 题目大意:对于给定的10*10的01矩阵,请问有多少个0被1包围了?(包围是指不能由上下左右通向边缘)本文来源于OIER博客,原文出处:http://www.oier.cc/ssoj2316%E9%9D%A2%E7%A7%AF/ 解题思路 图形学中Flood Fill是满水法填充,是用来填充区域的.就好比在一个地方一直到

[LeetCode] Flood Fill 洪水填充

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

733. Flood Fill

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

733. Flood Fill 简单型染色问题

[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newC