当前位置:网站首页>P1451 求细胞数量/1329:【例8.2】细胞
P1451 求细胞数量/1329:【例8.2】细胞
2022-07-05 14:47:00 【暴揍键盘的程序猿】
本程序猿在CSDN原力榜排名第15!!!

P1451 求细胞数量/1329:【例8.2】细胞
# 求细胞数量
## 题目描述
一矩形阵列由数字 $0$ 到 $9$ 组成,数字 $1$ 到 $9$ 代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。
## 输入格式
第一行两个整数代表矩阵大小 $n$ 和 $m$。
接下来 $n$ 行,每行一个长度为 $m$ 的只含字符 `0` 到 `9` 的字符串,代表这个 $n \times m$ 的矩阵。
## 输出格式
一行一个整数代表细胞个数。
## 样例 #1
### 样例输入 #1
```
4 10
0234500067
1034560500
2045600671
0000000089
```
### 样例输出 #1
```
4
```
## 提示
#### 数据规模与约定
对于 $100\%$ 的数据,保证 $1 \le n,m \le 100$。
1329:【例8.2】细胞
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 19435 通过数: 10979
【题目描述】
一矩形阵列由数字00到99组成,数字11到99代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。如:
阵列
4 10 0234500067 1034560500 2045600671 0000000089有44个细胞。
【输入】
第一行为矩阵的行nn和列mm;
下面为一个n×mn×m的矩阵。
【输出】
细胞个数。
【输入样例】
4 10
0234500067
1034560500
2045600671
0000000089【输出样例】
4【算法分析】
(1)从文件中读入n*m矩阵阵列,将其转换为boolean矩阵存入a数组中;
(2)沿a数组矩阵从上到下、从左到右,找到遇到的第一个细胞;
(3)将细胞的位置入队b,并沿其上、下、左、右四个方向上的细胞位置入队,入队后的位置a数组置为false;
(4)将b队的队头出队,沿其上、下、左、右四个方向上的细胞位置入队,入队后的位置a数组置为false;
(5)重复(4),直至b队空为止,则此时找出了一个细胞;
(6)重复(2),直至矩阵找不到细胞;
(7)输出找到的细胞数。
【AC代码(数组模拟队列)】
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std;
const int N=1e3+10;
inline int fread()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0' or ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0' and ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void fwrite(int n)
{
if(n>9)fwrite(n/10);
putchar(n%10+'0');
}
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},a[N][N],ans,n,m;
char ch[N],c;
void bfs(int x,int y)
{
int z,p,i=0,j=1,b[N][3];
ans++,a[x][y]=0,b[1][1]=x,b[1][2]=y;//遇到的第一个细胞入队
do
{
i++;//队头指针加1
for(int k=0;k<4;k++)//沿细胞的上下左右四个方向搜索细胞
{
z=b[i][1]+dx[k],p=b[i][2]+dy[k];
if(z>=0 and z<n and p>=0 and p<m and a[z][p])j++,b[j][1]=z,b[j][2]=p,a[z][p]=0;//判断该点是否可以入队
}
}while(i<j);//别漏了分号
}
signed main()
{
n=fread(),m=fread();
memset(a,1,sizeof a);//初始化
/*
另一种初始化的方式:
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)a[i][j]=1;
*/
for(int i=0;i<n;i++)
{
cin>>ch;
for(int j=0;j<m;j++)
if(ch[j]=='0')a[i][j]=0;
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(a[i][j])bfs(i,j);
fwrite(ans);
return 0;
}
//嘤嘤嘤这道万恶的bfs模板卡了我一整天
嘤嘤嘤这道万恶的bfs模板卡了我一整天


边栏推荐
- MySQL之CRUD
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- Penetration testing methodology
- Change multiple file names with one click
- 注意!软件供应链安全挑战持续升级
- 漫画:优秀的程序员具备哪些属性?
- 通过npm 或者 yarn安装依赖时 报错 出现乱码解决方式
- 计算中间件 Apache Linkis参数解读
- leetcode:881. 救生艇
- Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
猜你喜欢

Topology可视化绘图引擎

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
![[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)](/img/d7/f49bca8da2ce286c18508325985990.png)
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)

FR练习题目---简单题

Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel

Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management

Super wow fast row, you are worth learning!

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

Change multiple file names with one click

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
随机推荐
mysql8.0JSON_ Instructions for using contains
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
MongDB学习笔记
【招聘岗位】基础设施软件开发人员
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
安装配置Jenkins
[learning notes] connectivity and circuit of graph
Talking about how dataset and dataloader call when loading data__ getitem__ () function
Pointer operation - C language
GPS original coordinates to Baidu map coordinates (pure C code)
通过npm 或者 yarn安装依赖时 报错 出现乱码解决方式
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
[recruitment position] Software Engineer (full stack) - public safety direction
Share 20 strange JS expressions and see how many correct answers you can get
Reconnaissance des caractères easycr
be careful! Software supply chain security challenges continue to escalate
[learning notes] stage test 1
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
CODING DevSecOps 助力金融企业跑出数字加速度
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development