CodeForces - 616C(很有意思的bfs,set,map的使用)
传送门:
http://codeforces.com/problemset/problem/616/C
1 second
256 megabytes
standard input
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.
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.
Print the answer as a matrix as described above. See the examples to precise the format of the output.
3 3
*.*
.*.
*.*
3.3
.5.
3.3
4 5
**..*
..***
.*.*.
*.*.*
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的使用)的更多相关文章
- 一道很经典的 BFS 题
一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...
- Gym - 101291C (很有意思的最短路)
题意: 给出一张地图和机器人还有出口的位置,地图上面有障碍.然后给出UDLR上下左右四种指令,遇到障碍物或者越界的指令会忽略,剩下的继续执行. 只要到达出口就算找到出口,然后给你一串指令,让你修改指令 ...
- [Python][pythonchallenge][TBC]古老的python在线挑战赛,很有意思 (C0-C4)
预计阅读时间:15分钟 背景:搜索资料时候偶然发现的,很有意思,每一关都覆盖了很多知识点 Python版本:3.0 Talking is cheap,show me the code 主页: http ...
- 一行css代码调试中学到的javascript知识,很有意思
现在到处都是JavaScript,每天都能知道点新东西.一旦你入了门,你总能从这里或是那里领悟到很多知识.今天我想分享Addy Osmani的一行代码 ,这行代码对于你调试你的CSS是很有用的.为了可 ...
- We7——很有意思的一个开源CMS
目前做门户.做网站,基本上都需要用到一个系统,那就是CMS内容管理系统:现在开源产品有很多,笔者也是从事这个行业的,国内的各大CMS提供商基本上都试用过,今天向大家推荐一款很有意思的产品——We7CM ...
- 一道很有意思的java线程题
这几天看结城浩的<java多线程设计模式>,跟着做一些习题,有几道题目很有意思,记录下自己的体会. 首先是题目(在原书212页,书尾有解答): public class Main { pu ...
- 一种很有意思的数据结构:Bitmap
昨晚遇到了一种很有意思的数据结构,Bitmap. Bitmap,准确来说是基于位的映射.其中每个元素均为布尔型(0 or 1),初始均为 false(0).位图可以动态地表示由一组无符号整数构成的集合 ...
- 关于Java异常一段很有意思的代码
今天学习了Java的异常,讲到try-catch-finally时,老师演示了一段代码,觉得很有意思,很能反映出其执行的过程,让自己有点绕,特意记录一下. 只要代码执行到try代码内部, 不管有没有异 ...
- 一套很有意思的C语言测试题目
网络上逛博客,发现了一套很有意思的测试题目: https://kobes.ca/ 大家有兴趣可以做一下,考一些关于C语言使用的细节: 中文翻译参考: https://www.cnblogs.com/l ...
随机推荐
- CentOS7下配置FTP服务
1.参考教程: 腾讯云开发者实验室:基于 CentOS 搭建 FTP 文件服务 Linux就该这么学>:第11章 使用Vsftpd服务传输文件 (需要自己百度) 2.实验环境: VMware 1 ...
- 互联网轻量级框架SSM-查缺补漏第二天
简言:第一天没咋看,因为看的时候已经是下午了.今天上午也因为工作上的事没咋看,本来想按照天去写的,但是内容会太散吧.我决定把整块的内容放在一起写了.天数啥的,就那样把. 还有,我只是言简意赅的去总结一 ...
- Effective C++ .14 智能指针的拷贝与deleter函数
#include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...
- JavaScript实现邮箱后缀提示功能
先来个基础的 需求 根据下面需求实现如示意图所示的邮箱输入提示功能,注意,根据要求只需实现下面功能 当用户没有任何输入时,提示框消失 当用户输入字符后,显示提示框,并且把用户输入的内容自动拼上邮箱后缀 ...
- Python-约束和异常处理
今天我们来说一说类的规范以及程序出现错误后我们要怎么进行处理 一.类的约束 首先,你要清楚,约束是对类的约束,比如,现在你是一个项目经理,然后呢,你给手下的人分活,张三你处理一下普通用户登录,李四你处 ...
- Android 集成百度统计
在这里简单的介绍下怎么统计自己研发的APP 的用户活跃度,和使用量,以此来展示自己APP的用户使用量! 我们的APP都需要注入数据分析,以供我们实时的了解APP的下载和使用量提供了依据! 不过我还是更 ...
- web安全-XSS
了解XSS的定义 跨站脚本攻击(Cross Site Scripting),为了不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻 ...
- js 获取 网页屏幕高度 窗口高度 元素高度 滚动高度
常用: JS 获取浏览器窗口大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // 获取窗口宽度 if (window.innerWidth) winWidth = ...
- [翻译]Elasticsearch重要文章之二:堆内存的大小和swapping
Elasticsearch默认安装后设置的内存是1GB,对于任何一个业务部署来说,这个都太小了.如果你正在使用这些默认堆内存配置,你的集群配置可能有点问题. 这里有两种方式修改Elasticsearc ...
- QA-IDEA中用maven配置项目无法加载JDBC
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Im building Maven Java Web application and w ...