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.

Example

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

题意:这个题如果用普通的DFS的话对每个*遍历会TE。所以我们换个思路,不如先把每个.所在区域的大小DFS出来。然后再去遍历每个*,将其相连的.区域加起来(注意有的. 在同一区域,要用到map和set容器)

AC代码为:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std; int n, m;
char e[1005][1005];
int dir[4][2] = { 1,0,0,1,-1,0,0,-1};
int vis[1005][1005];
int cnt, num;
set<int> s;
map<int, int> mp; void dfs(int x, int y)
{
int i, j;
for (i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (tx <= 0 || tx > n || ty <= 0 || ty > m || vis[tx][ty] || e[tx][ty] == '*')
continue;
cnt++;
vis[tx][ty]=num;
dfs(tx, ty);
}
} int main()
{
int i, j;
num=0;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%s",e[i]+1); for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
if (!vis[i][j] && e[i][j] == '.')
{
num++;
cnt = 1;
vis[i][j]=num;
dfs(i, j);
mp[num] = cnt;
}
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (e[i][j] == '*')
{
int cc = 0;
s.clear(); for (int k = 0; k < 4; k++)
{
int tx = i + dir[k][0];
int ty = j + dir[k][1];
if (e[tx][ty] == '.' && !s.count(vis[tx][ty]))
{
s.insert(vis[tx][ty]);
cc += mp[vis[tx][ty]];
}
}
printf("%d", (cc + 1) % 10);
}
else
printf("%c", e[i][j]);
}
printf("\n");
}
return 0;
}

  

Coderfocers-616c的更多相关文章

  1. CodeForces 616C The Labyrinth

    先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #i ...

  2. CodeForces - 616C(很有意思的bfs,set,map的使用)

    传送门: http://codeforces.com/problemset/problem/616/C C. The Labyrinth time limit per test 1 second me ...

  3. LoRaWAN协议(三)--Server端数据协议

    LoRaWAN Server 端架构 LoRaWAN 的server包括 NS(Network server).AS(application server).CS(Custom server).... ...

  4. 使用SilverLight开发区域地图分析模块

    本人最近接收开发一个代码模块,功能主要是在页面上显示安徽省市地图,并且在鼠标移动到地图某市区域时,显示当前区域的各类信息等,一开始准备用百度地图,高德地图等地图工具进行开发,最后发现都不适合进行此类开 ...

  5. uboot中的mmc命令

    一:mmc的命令例如以下: 1:对mmc读操作 mmc read addr blk# cnt 2:对mmc写操作 mmc write addr blk# cnt 3:对mmc擦除操作 mmc eras ...

  6. tcpdump使用和TCP/IP包分析

    关于tcpdump如何抓包,本文不再总结,可以查看 tcpdump的官方地址查看http://www.tcpdump.org 本文重点记录两个部分:           第一部分:tcpdump所抓包 ...

  7. LoRaWAN协议(七)--完整数据流程

    以下的GW指Gateway 所用指令: root@lora-iot-sk:~# tcpdump -i lo -nn -x 'length>100' 入网流程 GW -> NS join_r ...

  8. cpp - 编译过程

    预处理 E:\CppSpace\hello>g++ -o main.i -E main.cpp E:\CppSpace\hello>dir /p 驱动器 E 中的卷是 固盘-项目 卷的序列 ...

  9. DM6446 uboot分析

    1. 顶层目录下的Makefile 按照配置顺序: davinci_config :    unconfig @./mkconfig $(@:_config=) arm arm926ejs davin ...

  10. 不为人知的网络编程(八):从数据传输层深度解密HTTP

    1.引言 在文章<理论联系实际:Wireshark抓包分析TCP 3次握手.4次挥手过程>中,我们学会了用wireshark来分析TCP的“三次握手,四次挥手”,非常好用.这就是传说中的锤 ...

随机推荐

  1. activmq点对点(简单写法)

    开发环境 我们使用的是ActiveMQ 5.11.1 Release的Windows版,官网最新版是ActiveMQ 5.12.0 Release,大家可以自行下载,下载地址. 需要注意的是,开发时候 ...

  2. iOS地理反地理编码--CoreLocation

    .sidebar{float:left;width:220px;} .container-fluid>.content{margin-left:240px;} a{color:#0069d6;t ...

  3. CSS(7)--- 通俗讲解清除浮动

    CSS(7)--- 通俗讲解清除浮动 上一篇讲了CSS浮动 博客地址:CSS(6)---通俗讲解浮动(float) 一.理解清除浮动 1.为什么要清除浮动 我们前面说过,浮动本质是用来做一些文字混排效 ...

  4. 半自动安装 linux 系统

    基于图形软件生成应答文件,实现半自动化安装 linux 操作系统 1.yum install -y system-config-kickstart 安装生成自动化安装操作系统的应答文件 2.syste ...

  5. 解放双手,在PC端进行Android真机调试

    scrcpy简介(拼写是scrcpy,非Python爬虫框架Scrapy) 简单地来说,scrcpy就是通过adb调试的方式来将手机屏幕投到电脑上,并可以通过电脑控制您的Android设备.它可以通过 ...

  6. 网站统计IP PV UV

    ###我只是一个搬运工 网站流量统计可以帮助我们分析网站的访问和广告来访等数据,里面包含很多数据的,比如访问使用的系统,浏览器,ip归属地,访问时间,搜索引擎来源,广告效果等. PV(访问量):Pag ...

  7. Cygwin安装教程

    cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件. 它对于学习unix/linux操作环境,或者从unix到windows的应用程序 ...

  8. PowerMock学习(七)之Mock Constructor的使用

    前言 我们在编码的时候,总习惯在构造器中传参数,那么在powermock中是怎么模拟带参数构造的呢,这并不难. 模拟场景 我们先模拟这样一个场景,通过dao中的传入一个是布尔类型(是否加载)和一个枚举 ...

  9. Python函数的默认参数的设计【原创】

    在Python教程里,针对默认参数,给了一个“重要警告”的例子: def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2)) print( ...

  10. C#学习笔记03--循环和一维数组

    一.循环(重点) 什么时候用循环? 想让一段代码执行多次, 这段代码可能不一样但是一定有一个规律. 1.while 循环 格式:  while(循环条件) { 循环执行的代码; } 循环的机制:  当 ...