传送门:

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

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

Copy
3 3
*.*
.*.
*.*
Output

Copy
3.3
.5.
3.3
Input

Copy
4 5
**..*
..***
.*.*.
*.*.*
Output

Copy
46..3
..732
.6.4.
5.4.3 题目意思:
求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出 分析:
开始是想对每个*号进行bfs,但是思考了一下,这种做法态蠢了,也耗时
冥思苦想无果,看了一下xp大佬的博客
得到了思路:
对‘.’号进行遍历扩展,得到每个连通块的标号和大小,标号用vis数组储存
map储存标号对应的连通块的大小,然后对每个*号,把它上下左右的连通块的标号放入set中(去重重复的连通块标号,这里注意理解)
然后在map中找到对应标号的连通块大小,加起来之后加1(*自身)就是该*号周围的.号的个数
code:
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
#include<map>
#include<set>
using namespace std;
#define max_v 1005
char G[max_v][max_v];
int vis[max_v][max_v];//记录连通块的标号
int n,m; struct node
{
int x,y;
}; queue<node> q;
map<int,int> mm;//记录对应标号连通块的大小
set<int> ss;//去除*号周围重复的标号的连通块
set<int>::iterator it;
int dir[][]= {,,,,-,,,-}; int check(int x,int y)
{
if(x<||x>=n||y<||y>=m)
return ;
if(vis[x][y])
return ;
if(G[x][y]!='.')
return ;
return ;
}
void bfs(int x,int y,int cnt)
{
node p,next;
vis[x][y]=cnt;
int ans=; p.x=x;
p.y=y;
q.push(p); while(!q.empty())
{
ans++;
p=q.front();
q.pop(); for(int i=; i<; i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][]; if(check(next.x,next.y))
{
vis[next.x][next.y]=cnt;
q.push(next);
}
}
}
mm[cnt]=ans;
// printf("%d-->%d\n",cnt,ans);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=; i<n; i++)
scanf("%s",G[i]); memset(vis,,sizeof(vis)); int cnt=;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(G[i][j]=='.'&&vis[i][j]==)
{
bfs(i,j,cnt++);//得到连通块的标号和大小
}
}
} for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(G[i][j]=='*')
{
ss.clear(); //将*周围连通块的标号放入set去重
if(i>)
ss.insert(vis[i-][j]);
if(i<n-)
ss.insert(vis[i+][j]);
if(j>)
ss.insert(vis[i][j-]);
if(j<m-)
ss.insert(vis[i][j+]); int res=;
for(it=ss.begin(); it!=ss.end(); it++)
{
res+=mm[*it];//通过map找到对应标号连通块的大小
// printf("set中值:%d map中的值:%d\n",*it,mm[*it]);
} // printf("res=%d\n",res);
G[i][j]=char(''+res%);
}
}
}
for(int i=; i<n; i++)
{
printf("%s\n",G[i]);
}
}

CodeForces - 616C(很有意思的bfs,set,map的使用)的更多相关文章

  1. 一道很经典的 BFS 题

    一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...

  2. Gym - 101291C (很有意思的最短路)

    题意: 给出一张地图和机器人还有出口的位置,地图上面有障碍.然后给出UDLR上下左右四种指令,遇到障碍物或者越界的指令会忽略,剩下的继续执行. 只要到达出口就算找到出口,然后给你一串指令,让你修改指令 ...

  3. [Python][pythonchallenge][TBC]古老的python在线挑战赛,很有意思 (C0-C4)

    预计阅读时间:15分钟 背景:搜索资料时候偶然发现的,很有意思,每一关都覆盖了很多知识点 Python版本:3.0 Talking is cheap,show me the code 主页: http ...

  4. 一行css代码调试中学到的javascript知识,很有意思

    现在到处都是JavaScript,每天都能知道点新东西.一旦你入了门,你总能从这里或是那里领悟到很多知识.今天我想分享Addy Osmani的一行代码 ,这行代码对于你调试你的CSS是很有用的.为了可 ...

  5. We7——很有意思的一个开源CMS

    目前做门户.做网站,基本上都需要用到一个系统,那就是CMS内容管理系统:现在开源产品有很多,笔者也是从事这个行业的,国内的各大CMS提供商基本上都试用过,今天向大家推荐一款很有意思的产品——We7CM ...

  6. 一道很有意思的java线程题

    这几天看结城浩的<java多线程设计模式>,跟着做一些习题,有几道题目很有意思,记录下自己的体会. 首先是题目(在原书212页,书尾有解答): public class Main { pu ...

  7. 一种很有意思的数据结构:Bitmap

    昨晚遇到了一种很有意思的数据结构,Bitmap. Bitmap,准确来说是基于位的映射.其中每个元素均为布尔型(0 or 1),初始均为 false(0).位图可以动态地表示由一组无符号整数构成的集合 ...

  8. 关于Java异常一段很有意思的代码

    今天学习了Java的异常,讲到try-catch-finally时,老师演示了一段代码,觉得很有意思,很能反映出其执行的过程,让自己有点绕,特意记录一下. 只要代码执行到try代码内部, 不管有没有异 ...

  9. 一套很有意思的C语言测试题目

    网络上逛博客,发现了一套很有意思的测试题目: https://kobes.ca/ 大家有兴趣可以做一下,考一些关于C语言使用的细节: 中文翻译参考: https://www.cnblogs.com/l ...

随机推荐

  1. java导入excle表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作

    1.首先创建一个java项目 完成效果如下图所示 2.导入以下jar包 3.代码如下 其中行和列的操作是根据需求自动划分的 public class auto_date { private stati ...

  2. 前端(一):html标签

    HTML(Hypertext Markup Language)超文本标记语言,它负责页面的结构.超文本指的是超链接,使用超链接可以从一个页面跳转到另一个页面. HTML的发展:1993年6月发布第一个 ...

  3. es入门教程

    因为项目可能会用到es保存一些非结构化的数据,并从中检索数据.对es调研了一下 从官网:https://www.elastic.co/downloads下载,解压即安装. 进入解压目录,执行bin目录 ...

  4. 键盘录入(Java)

    键盘录入(Java): 1.导包 格式 import java.util.Scanner; 位置 在class上面 2.创建键盘录入对象 格式 Scanner sc = new Scanner(Sys ...

  5. Effective C++ .12 复制对象-拷贝构造函数的编写

    当我们自己编写拷贝构造函数时,编译器就不会为该类生成默认拷贝构造函数了,对于assignment operator也是如此. 1. 拷贝构造函数中记得调用父类的拷贝构造函数,或者相应复制过程 clas ...

  6. oracle 基础笔记

    sqlplus / as sysbda;-- 运行命令conn scott/tiger [as sysdba];-- 登录show user;查看当前用户alter user scott accoun ...

  7. MySQL行(记录)详细操作

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  8. react 使用fortawesome字体图标

    fontawesome 官方使用教程=>点我 npm i --save @fortawesome/fontawesome-svg-core@prerelease \ npm i --save @ ...

  9. 向Github提交更改的代码

    更改了本地的某一文件的代码,那么如何覆盖Github上的同一文件代码呢?请看以下步骤: 1.先用 git status 看你更改了哪些文件: 2.然后 git add 你想要提交的更改的文件 或者 g ...

  10. C#启动外部程序(进程)

    通过调用Process类可以启动系统内部(环境变量里的)或者指定位置的程序,例如: Process.Start("notepad");//启动记事本 Process.Start(& ...