传送门:

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. 00HTML

    一.概述 超文本标记语言(Hyper Text Markup Language),HTML是一门描述性的语言.基本语法: <标签> 内容 </标签>** 在一个网页中,HTML ...

  2. 初识shell expect

    场景:工作中经常会遇到shell脚本写的连接脚本,所以稍微了解下. 一.shell Shell 是一个用C语言编写的程序,它是用户使用Linux的桥梁.Shell既是一种命令语言,又是一种程序设计语言 ...

  3. 机智的Popup,带着简单的图表感觉酷酷的

    之前有提过用 InfoTemplate 类对 FeatureLayer 的基本信息进行展示,今天为大家介绍 esri/dijit/Popup 的使用,这东西还有 简单的图表展示功能呢! <!DO ...

  4. Codeforces Round #411 (Div. 2) C. Find Amir

    C. Find Amir time limit per test   1 second memory limit per test   256 megabytes   A few years ago ...

  5. 寒假来了,阿里游戏云6000、20000元新春大礼,游戏开发的骚年们r u ready?

    寒假来了,游戏开发的骚年们,r u ready? 亿元云计算基金.游戏云计算解决方案.尊享VIP服务,为你“三羊开泰”! 现在参与游戏云认证,即享6000元.2万元… 最高100万云基金!走你> ...

  6. 【Android】移动项目位置

    一.前言 有时候,我们新建一个项目后,因为一些原因想要重新移动项目到别的文件夹.然而,移动到新的文件夹后,却发现之前好好的项目运行不了.点击运行后,弹出如下对话框.  Installation fai ...

  7. Qt判断鼠标在控件上

    QT判断鼠标是否在某子窗口控件上方 需要注意的是,子窗口获取geometry,是相对于父窗口的相对位置,QCursor::pos()获取的是鼠标绝对位置,要不将父窗口的相对位置进行换算,要不将鼠标的绝 ...

  8. scrum心得和团队作业

    一.学习scrum心得 敏捷的介绍 最近上课我们了解到了敏捷,很多人开始谈论敏捷开发.研究敏捷开发,那么究竟什么才是敏捷开发呢? 简单的说,敏捷开发是一种以人为核心.迭代.循序渐进的开发方法.在敏捷开 ...

  9. java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.

    1.前几天搭建单元测后,今天用其测试,结果报了这个问题.网上搜索后,刚开始以为原因是  Spring的 依赖版本的问题,我现在的依赖是: 因为其他的比如说 spring-content  spring ...

  10. xlwings: Write Excel macro using python instead of VBA

    i want to write Excel macros to deal with the data, but i am not familiar with VBA language. so i de ...