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 ...
随机推荐
- Springmvc file多附件上传 显示 删除操作
之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...
- 解锁Spring框架姿势1
Spring 介绍:Spring 框架是一个Java平台,它为开发Java应用程序提供全面的基础架构支持.Spring负责基础架构,因此您可以专注于应用程序的开发. Spring可以让您从" ...
- java四大特性详解
Java的四大基础特性一.抽象 父类为子类提供一些属性和行为,子类根据业务需求实现具体的行为. 抽象类使用abstract进行修饰,子类要实现所有的父类抽象方法否则子类也是抽象类.二.封装 把对象的属 ...
- Filter的常见应用
1.字符编码过滤器 实现功能,在a.jsp中填写用户名提交到b.jsp,在b.jsp中读取参数名. a.jsp <body> <form action="encoding/ ...
- SPDY和HTTP
SPDY 是什么 ? SPDY 是 Google 开发的基于传输控制协议 (TCP) 的应用层协议.SPDY 协议旨在通过压缩.多路复用和优先级来缩短网页的加载时间和提高安全性.(SPDY 是 Spe ...
- int btn = (Button) findViewById(View.getId());
int btn = (Button) findViewById(View.getId());//这句话中的btn不能用来和按钮键Button的id号去比较 如果想存储Button,可以这样做: Sta ...
- Algorithm——两数之和
题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...
- Hello Activemq
0. 如果永远是localhost 可能一直low下去 1.下载安装 activemq 1.1 从官网下载activemq.tar.gz 并上传(rz)到linux系统 并解压 tar zxvf /* ...
- DB DBS 和DBMS区别
DB:是指datebase(数据库) DBS:是指datebase systerm (数据库系统) DBMS:是指datebase mangement systerm(数据库管理系统)区别:数据库 ...
- Java 监听器,国际化
1. 监听器 1.1 概述 监听器: 主要是用来监听特定对象的创建或销毁.属性的变化的! 是一个实现特定接口的普通java类! 对象: 自己创建自己用 (不用监听) 别人创建自己用 (需要监听) Se ...