【CodeForces - 616C】The Labyrinth(bfs,并查集,STLset)

题干:

求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出。

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.

Examples

Input

3 3
*.*
.*.
*.*

Output

3.3
.5.
3.3

Input

4 5
**..*
..***
.*.*.
*.*.*

Output

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).

解题报告:

   直接对每个'.'进行搜索看连通块,然后对每一个'*'直接求答案就行了,注意不要算重复,所以需要set去判重。(因为有可能四个方向属于同一个联通块,所以不判重就会加四次)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 1000 + 5;
char maze[MAX][MAX];
int ans[MAX][MAX];
bool vis[MAX][MAX];
int f[MAX*MAX];
int num[MAX*MAX]; 
int n,m;
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
int getf(int v) {return v == f[v] ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);f[t2] = t1;
}
int get(int x,int y) {return (x-1)*m +y;
}
void go(int x,int y) {for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(tx < 1 || tx > n || ty < 1 || ty > m) continue;if(maze[tx][ty] == '*' || vis[tx][ty]) continue;merge(get(x,y),get(tx,ty));vis[tx][ty] = 1;go(tx,ty);}
}
int main()
{cin>>n>>m;for(int i = 0; i<=n*m; i++) {f[i] = i;}for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.' && vis[i][j] == 0) {vis[i][j] = 1;//别忘这步!!虽然这题不会WA但是不写是错的。go(i,j);}			}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '*') continue;num[getf(get(i,j))]++;}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.') ans[i][j] = '.';else {int cnt = 0;set<int> ss;for(int k = 0; k<4; k++) {int tx = i + nx[k];int ty = j + ny[k];if(maze[tx][ty] == '*')continue;if(tx < 1 || tx > n || ty < 1 || ty > m) continue;ss.insert(getf(get(tx,ty)));}auto it = ss.begin();for(;it!=ss.end();++it) cnt += num[*it];ans[i][j] = cnt;}}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '.') printf(".");else printf("%d",(ans[i][j]+1)%10);}puts("");}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439995.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

scapy和dpkt使用

scapy官方文档 Scapy 下载 # (临时换pip源) pip install scapy (-i https://pypi.tuna.tsinghua.edu.cn/simple/)导入 from scapy.all import *读取pcap文件&#xff0c;进行相关操作 # 读取文件 # 整个文件&#xff1a;packets&#xff1a;scapy.plist.PacketList对象 &…

Google Colab——谷歌免费GPU使用教程

Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具&#xff0c;主要用于机器学习的开发和研究。这款工具现在可以免费使用。Google Colab最大的好处是给广大的AI开发者提供了免费的GPU使用&#xff01;GPU型号是Tesla K80&#xff01;你可以在上面轻松地跑例如&am…

javaBean和Servlet的区别

可以像使用一般的类一样使用JavaBean,Bean只是一种特殊的类。特殊在可以通过<jsp:useBean />调用JavaBean。而其他类,可以和一般java中一样使用。 Bean的参数中还可以指定范围, <jsp:useBean scope"application" />该Bean在服务器的JVM中将只有一个…

pyecharts简单使用

pyecharts 是一个用于生成 Echarts 图表的类库。 Echarts 是百度开源的一个数据可视化 JS 库。可以生成很多效果很棒的图表。 pycharts文档 |分割| echarts官网 本文主要介绍pycharts的简单使用 安装 # 安装 1.0.x 以上版本 &#xff08;需要python3.6及以上&#xff09; $ …

【POJ - 2373】Dividing the Path(单调队列优化dp)

题干&#xff1a; Farmer Johns cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers along the ridge of the hill. To make installa…

Ubuntu16.4(64位)下gcc-linaro-arm-linux-gnueabihf交叉编译环境安装

1. 下载压缩包 文件分享 2. 新建目录并解压 3. 配置环境变量 sudo gedit /etc/bash.bashrc 添加路径并更新路径&#xff1a;&#xff08;PATH$PATH之间无空格&#xff09; PATH$PATH://linaro-arm/gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/binexport P…

JAVA高级工程师课程笔记整理——(八)tomcat与九大内置对象

&#xff08;八&#xff09;tomcat与九大内置对象 tomcat B/S 浏览器/服务器 请求&#xff1a;request 响应: response C/S&#xff1a; 客户端/服务器 URL: 网址 URI: 范围包括url http https: 更安…

tshark 小技巧

将pcap转换成json文件&#xff0c;全部特征名都会在json中 tshark -T json -r D:\test.pcap > test.json合并数据包 mergecap -w all.pcap 1.pcap 2.pcap ...all.pcap: 合并之后的数据包 1.pcap ... 要合并的数据包&#xff0c;可以合并n个

【CodeForces - 1201C】Maximum Median(思维,水题)

题干&#xff1a; You are given an array aa of nn integers, where nn is odd. You can make the following operation with it: Choose one of the elements of the array (for example aiai) and increase it by 11(that is, replace it with ai1ai1). You want to make …

Apollo进阶课程 ④ | 开源模块讲解(下)

目录 1&#xff09;Apollo平台技术框架 2&#xff09;Apollo版本迭代 原文链接&#xff1a;​Apollo进阶课程 ④ | 开源模块讲解&#xff08;下&#xff09; 上周&#xff0c;阿波君与大家讨论了自动驾驶的核心问题——安全性。本期&#xff0c;我们将为大家具体介绍百度Apo…

SM4 简介

SM4 我国国家密码管理局在20012年公布了无线局域网产品使用的SM4密码算法——商用密码算法。它是分组算法当中的一种&#xff0c;算法特点是设计简沽&#xff0c;结构有特点&#xff0c;安全高效。数据分组长度为128比特&#xff0c;密钥长度为128 比特。加密算法与密钥扩展算法…

九大内置对象

指在JSP的<%%> 和<% %>中可以直接使用的对象&#xff1a;没有特别说明可以开关的默认是开启的 一servlet理论上可以处理多种形式的请求响应形式http只是其中之一所以HttpServletRequest HttpServletResponse分别是ServletRequest和ServletResponse的之类 二 Http…

3)机器学习基石笔记 Lecture3:Types of Learning

目录 1&#xff09;Learning with Different Output Space Y 2&#xff09;Learning with Different Data Label 3&#xff09;Learning with Different Protocol 4&#xff09;Learning with Different Input Space X 在上一节课中&#xff0c;我们学到了第一个机器学习…

【BZOJ - 3436】小K的农场(差分约束)

题干&#xff1a; 背景 小K是个特么喜欢玩MC的孩纸。。。 描述 小K在MC里面建立很多很多的农场&#xff0c;总共n个&#xff0c;以至于他自己都忘记了每个农场中种植作物的具体数量了&#xff0c;他只记得 一些含糊的信息&#xff08;共m个&#xff09;&#xff0c;以下列…

分组密码简介和五大分组模式

分组密码 分组密码&#xff08;blockcipher&#xff09;是每次只能处理特定长度的一块数据的一类密码算法&#xff0c;这里的一块"就称为分组&#xff08;block&#xff09;。此外&#xff0c;一个分组的比特数就称为分组长度&#xff08;blocklength&#xff09;。例如&…

Java Web(五) JSP详解(四大作用域九大内置对象等)

前面讲解了Servlet&#xff0c;了解了Servlet的继承结构&#xff0c;生命周期等&#xff0c;并且在其中的ServletConfig和ServletContext对象有了一些比较详细的了解&#xff0c;但是我们会发现在Servlet中编写一些HTML代码&#xff0c;是很不方便的一件事情&#xff0c;每次都…

Apollo进阶课程 ⑤ | Apollo硬件开发平台介绍

目录 1&#xff09;Uber事故原因分析 2&#xff09;自动驾驶的第一天条-----安全 3&#xff09;自动驾驶汽车的硬件系统 4&#xff09;自动驾驶汽车感知类传感器介绍 5&#xff09;自动驾驶汽车的传感器 6&#xff09;自动驾驶的计算单元 7&#xff09;自动驾驶的线控系…

【HDU - 3440】House Man(差分约束)

题干&#xff1a; In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump tak…

使用tcpdump,adb进行手机抓包

准备 手机 root PC安装ADB 下载压缩包&#xff0c;解压即可 链接&#xff1a;https://pan.baidu.com/s/1Hv-IqpQutBVTHuriakQUTg 提取码&#xff1a;q57q 配置环境变量 在系统环境Path中添加 adb.exe 的地址 验证安装 adb version 出现版本&#xff0c;即为成功 开启adb服…

依赖注入和控制反转的理解,写的太好了。

学习过Spring框架的人一定都会听过Spring的IoC(控制反转) 、DI(依赖注入)这两个概念&#xff0c;对于初学Spring的人来说&#xff0c;总觉得IoC 、DI这两个概念是模糊不清的&#xff0c;是很难理解的&#xff0c;今天和大家分享网上的一些技术大牛们对Spring框架的IOC的理解以及…