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 ...
随机推荐
- 译:Java局部变量类型推断(Var类型)的26条细则
原文链接:https://dzone.com/articles/var-work-in-progress 作者:Anghel Leonard 译者:沈歌 Java局部变量类型推断(LVTI),简称va ...
- Bazaar 版本控制工具
Bazaar是一个分布式的版本控制系统,它发布在GPL许可协议之下,并可用于Windows.GNU/Linux.UNIX以及Mac OS系统.Bazaar由Canonical公司赞助,目前已服务于Sa ...
- jQuery知识点学习整理
零.jQuery中操作css的方法 1.$("p").css("background-color"); 返回首个匹配元素的background-color的值. ...
- String Control
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- 从零开始的全栈工程师——html篇1.7
position定位与表单 一.position 1.Position细说 Position:relative; Left:100px; Top:100px; Position:absolute; L ...
- BZOJ4698: Sdoi2008 Sandy的卡片(二分 hash)
题意 题目链接 Sol 用什么后缀数组啊 直接差分之后 二分+hash找最长公共子串就赢了啊... 时间复杂度:\(O(nlogn)\)(不过我写的是两个log..反正也能过) // luogu-ju ...
- Redis学习笔记(一) ---- Linux系统中部署Redis存储系统
Redis 一.Redis简介 1.Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合 ...
- vscode自定义代码块
vscode中设置自定义代码块打开首选项,选择用户代码片段,打开后选择编程语言选中后打开文件,按照格式编辑内容 "Print to console log": { "pr ...
- 安装配置postgreSQL+pgcli+pgadmin3
记录了postgreSQL数据库的完整的安装配置过程,以及postgreSQL的pgcli命令行智能提醒扩展,pgadmin3图形化管理客户端的配置安装.此postgresql是bigsql版安装详情 ...
- 在Ubuntu14.10中部署Hadoop2.6.0单节点伪分布集群
1. 环境信息如下: ubuntu:14.10 jdk:openjdk-1.7.0 hadoop:2.6.0 2. 下载hadoop2.6.0, http://apache.fayea.com/had ...