当前位置:网站首页>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模板卡了我一整天


边栏推荐
- webRTC SDP mslabel lable
- C language -- structure and function
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- 超级哇塞的快排,你值得学会!
- Countermeasures of enterprise supply chain management system in UCA Era
- 选择排序和冒泡排序
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- 启牛学堂班主任给的证券账户安全吗?能开户吗?
- [12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
- PHP - fatal error: allowed memory size of 314572800 bytes exhausted
猜你喜欢
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
Security analysis of Web Architecture
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
一键更改多个文件名字
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
计算中间件 Apache Linkis参数解读
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
Fr exercise topic --- comprehensive question
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
随机推荐
Two Bi development, more than 3000 reports? How to do it?
How to solve the problem of garbled code when installing dependency through NPM or yarn
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
Anaconda uses China University of science and technology source
Is it OK to open the securities account on the excavation finance? Is it safe?
基于TI DRV10970驱动直流无刷电机
TS所有dom元素的类型声明
webRTC SDP mslabel lable
启牛学堂班主任给的证券账户安全吗?能开户吗?
Long list optimized virtual scrolling
12 MySQL interview questions that you must chew through to enter Alibaba
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Stm32+bh1750 photosensitive sensor obtains light intensity
CODING DevSecOps 助力金融企业跑出数字加速度
be careful! Software supply chain security challenges continue to escalate
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
MongDB学习笔记
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection