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的“三次握手,四次挥手”,非常好用.这就是传说中的锤 ...
随机推荐
- jade 学习笔记 - gulp 自动编译
实时监控 jade -P -w .\test1.jade sublime 分栏,可以看到实时修改情况 1. 元素写法 doctype html <!--[if IE8]>< ...
- django 之创建自己的模板(使用案例)
Django 创建自己的模板篇(实例) 此处需要创建模板,主要是对自己的模板进行扩展: 一般是扩展模板的tag和filter两个功能.可以用来创建你自己的tag和filter功能库. 创建模板库 分为 ...
- Python 面向对象之五 基础拾遗
Python 面向对象之五 基础拾遗 今天呢,就剩下的面向对象的相关知识进行学习,主要会学习以下几个方面的知识:1.上下文管理协议,2.为类加装饰器 3.元类 一.上下文管理协议 在学习文件操作的时候 ...
- IDEA+JSP+Servlet+Tomcat简单的登录示例
1.用IDEA新建Java WEB项目并配置Tomcat 这一部分可以参考之前的一篇随笔 https://www.cnblogs.com/lbhym/p/11496610.html 2.导入Servl ...
- 使用JSP脚本在页面输出九九乘法表
<% int i,j; for(i=1;i<10;i++) { for(j=1;j<=i;j++) { out.println(i+"*"+j+"=&q ...
- Linux安装telnet C/S 【白话文】
1.安装telnet 和telnet-server yum -y install telnet yum -y install telnet-server 注意:在此安装过程中,会依赖解决xinetd的 ...
- Bran的内核开发教程(bkerndev)-08 中断服务程序(ISR)
中断服务程序(ISR) 中断服务程序(ISR)用于保存当前处理器的状态, 并在调用内核的C级中断处理程序之前正确设置内核模式所需的段寄存器.而工作只需要15到20行汇编代码来处理, 包括调用C中的 ...
- vue中自定义html文件的模板
如果默认生成的 HTML 文件不适合需求,可以创建/使用自定义模板. 一是通过 inject 选项,然后传递给定制的 HTML 文件.html-webpack-plugin 将会自动注入所有需要的 C ...
- 20191121-6 Scrum立会报告+燃尽图 02
此作业的要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10066一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩 ...
- python3 之 闭包实例解析
一.实例1: def make_power(y): def fn(x): return x**y return fn pow3 = make_power(3) pow2 = make_power(2) ...