Coderfocers-616c
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 nstrings 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.
Example
3 3
*.*
.*.
*.*
3.3
.5.
3.3
4 5
**..*
..***
.*.*.
*.*.*
46..3
..732
.6.4.
5.4.3
Note
In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).
题意:这个题如果用普通的DFS的话对每个*遍历会TE。所以我们换个思路,不如先把每个.所在区域的大小DFS出来。然后再去遍历每个*,将其相连的.区域加起来(注意有的. 在同一区域,要用到map和set容器)
AC代码为:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std; int n, m;
char e[1005][1005];
int dir[4][2] = { 1,0,0,1,-1,0,0,-1};
int vis[1005][1005];
int cnt, num;
set<int> s;
map<int, int> mp; void dfs(int x, int y)
{
int i, j;
for (i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (tx <= 0 || tx > n || ty <= 0 || ty > m || vis[tx][ty] || e[tx][ty] == '*')
continue;
cnt++;
vis[tx][ty]=num;
dfs(tx, ty);
}
} int main()
{
int i, j;
num=0;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%s",e[i]+1); for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
if (!vis[i][j] && e[i][j] == '.')
{
num++;
cnt = 1;
vis[i][j]=num;
dfs(i, j);
mp[num] = cnt;
}
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (e[i][j] == '*')
{
int cc = 0;
s.clear(); for (int k = 0; k < 4; k++)
{
int tx = i + dir[k][0];
int ty = j + dir[k][1];
if (e[tx][ty] == '.' && !s.count(vis[tx][ty]))
{
s.insert(vis[tx][ty]);
cc += mp[vis[tx][ty]];
}
}
printf("%d", (cc + 1) % 10);
}
else
printf("%c", e[i][j]);
}
printf("\n");
}
return 0;
}
Coderfocers-616c的更多相关文章
- CodeForces 616C The Labyrinth
先预处理出所有连通块,对于每一个*,看他四周的连通块即可 #include<cstdio> #include<cstring> #include<queue> #i ...
- CodeForces - 616C(很有意思的bfs,set,map的使用)
传送门: http://codeforces.com/problemset/problem/616/C C. The Labyrinth time limit per test 1 second me ...
- LoRaWAN协议(三)--Server端数据协议
LoRaWAN Server 端架构 LoRaWAN 的server包括 NS(Network server).AS(application server).CS(Custom server).... ...
- 使用SilverLight开发区域地图分析模块
本人最近接收开发一个代码模块,功能主要是在页面上显示安徽省市地图,并且在鼠标移动到地图某市区域时,显示当前区域的各类信息等,一开始准备用百度地图,高德地图等地图工具进行开发,最后发现都不适合进行此类开 ...
- uboot中的mmc命令
一:mmc的命令例如以下: 1:对mmc读操作 mmc read addr blk# cnt 2:对mmc写操作 mmc write addr blk# cnt 3:对mmc擦除操作 mmc eras ...
- tcpdump使用和TCP/IP包分析
关于tcpdump如何抓包,本文不再总结,可以查看 tcpdump的官方地址查看http://www.tcpdump.org 本文重点记录两个部分: 第一部分:tcpdump所抓包 ...
- LoRaWAN协议(七)--完整数据流程
以下的GW指Gateway 所用指令: root@lora-iot-sk:~# tcpdump -i lo -nn -x 'length>100' 入网流程 GW -> NS join_r ...
- cpp - 编译过程
预处理 E:\CppSpace\hello>g++ -o main.i -E main.cpp E:\CppSpace\hello>dir /p 驱动器 E 中的卷是 固盘-项目 卷的序列 ...
- DM6446 uboot分析
1. 顶层目录下的Makefile 按照配置顺序: davinci_config : unconfig @./mkconfig $(@:_config=) arm arm926ejs davin ...
- 不为人知的网络编程(八):从数据传输层深度解密HTTP
1.引言 在文章<理论联系实际:Wireshark抓包分析TCP 3次握手.4次挥手过程>中,我们学会了用wireshark来分析TCP的“三次握手,四次挥手”,非常好用.这就是传说中的锤 ...
随机推荐
- Linux系统中nc工具那些不为人知的用法
Linux nc命令用法 参考地址:https://www.cnblogs.com/jjzd/p/6306273.html -g<网关>:设置路由器跃程通信网关,最多设置8个; -G< ...
- 创建基于OData的Web API - Knowledge Builder API, Part I:Business Scenario
在.NET Core 刚刚1.0 RC的时候,我就给OData团队创建过Issue让他们支持ASP.NET Core,然而没有任何有意义的答复. Roadmap for ASP.NET Core 1. ...
- Spring-boot构建多模块依赖工程时,maven打包异常:程序包xxx不存在
在qizhi项目改版的时候, 所有代码都迁移好了, 但是compile的时候报程序包*****不存在, 具体到某一个类就是: 找不到符号. 下面这篇文章是正解 http://hbxflihua.ite ...
- Mybatis动态SQL(where元素、set元素、if元素)
Mybatis动态SQL(where元素.set元素.if元素) - where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句.而且,若语句的开头为“AND”或 ...
- 如何使用Sping Data JPA更新局部字段
问题描述 在更新数据时,有时候我们只需要更新一部分字段,其他字段保持不变.Spring Data JPA并未提供现成的接口,直接使用save()更新会导致其他字段被Null覆盖掉. 解决办法 通常有两 ...
- scipy.sparse的csc_matrix、csr_matrix与coo_matrix区别与应用(思维导图)
- Future模式的学习以及JDK内置Future模式的源码分析
并发程序设计之Future模式 一).使用Future模式的原因 当某一段程序提交了一个请求,期待得到一个答复,但服务程序对这个请求的处理可能很慢,在单线程的环境中,调用函数是同步的,必须等到服务程序 ...
- vue—自定义指令
今日分享—自定义指令 需要学习的点: modifiers属性的具体实例就是v-on:click.stop=”handClick” 一样,为指令添加一个修饰符. 全局指令:新建一个newDir.js i ...
- [Odoo12基础教程]之开发过程中可能出现的问题
可能出现的问题 更改代码后无变化 当你对代码进行更改之后,发现页面并没有变化,那么请尝试依次以下几种办法: 1.重启项目: 2.升级模块: 3.在开发者模式下刷新本地模块列表: 4.给data列表添加 ...
- 2019-9-23:渗透测试,基础学习,http协议数据包的认识,html css的认识,笔记
Burp suite功能模块Dashboard:扫描Proxy:拦截包,代理 drop:放弃Intruder:爆破Decoder:编码,解码repeater:重放comparer:比较 BP,prox ...