1329:【例8.2】细胞 【广度优先搜索】

首先你需要知道什么是队列:

由于手搓数组队列比较浪费初学者的时间,建议优先掌握STL大法中的queue队列模板

具体实现:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    queue<int> q;//申请了一个名叫q的队列
	//请问此时q的大小是多少? 
	cout<<q.size()<<endl;
	//是否为空? 返回真   size()返回非0不空,返回0就是空 
	cout<<q.empty()<<endl;
	q.push(66); //  从尾部压入66
	q.push(88);//   66 88
	q.push(1);//    66 88 1
	cout<<q.front()<<" "<<q.back()<<endl;
	//输出(弹出)队列中的所有元素 
	while( q.size() ){ //!q.empty()
		cout<<q.front()<<" ";
		q.pop();//从头部弹出 
	}
	cout<<"此时队列为空";  
	return 0;
}

课堂练习:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	queue<char> q;//创建一个字符队列
	q.push('A');
	cout<<q.size()<<endl;//输出1
	q.pop(); //将头部的'A'弹出 
	q.push('B');
 	q.push('B');
	q.push('C');
	q.pop();
	q.push('V');
	//BCV  
	while( q.size() ){//弹出所有元素 并打印 
		cout<<q.front();
		q.pop();
	}
	return 0;
}

例题:1329:【例8.2】细胞

#include<bits/stdc++.h>
using namespace std;
const int N=100;
char mp[N][N];
int n,m,cnt,vis[4][2]={1,0,-1,0,0,-1,0,1}; 
bool st[N][N];//st[i][j]==1 表示第i行第j列的细胞被染色了(烧过了) 
struct node{
	int x,y;
};

void bfs(int x,int y){
	queue<node> q;
	q.push({x,y});//起点入队
	while( q.size() ){
		node tp=q.front();//将队头取出
		q.pop();
		//将当前这个点能够波及到的所有点入队
		for(int i=0;i<4;i++)
		{
			int tx=tp.x+vis[i][0],ty=tp.y+vis[i][1];
			//当前这个是否可以被波及 
			//没越界+是细胞      &&   没染过色 
			if(mp[tx][ty]>='1'&&mp[tx][ty]<='9' && st[tx][ty]==0 ){
				st[tx][ty]=1;
				q.push({tx,ty});//入队! 
			} 
		 } 
	} 
}

int main()
{
	cin>>n>>m;//行 列 
	for(int i=1;i<=n;i++)//二维数字矩阵的读入 
	    for(int j=1;j<=m;j++)
	        cin>>mp[i][j];
    //非零 并且 未染色st[][]==0 
    for(int i=1;i<=n;i++)
	   for(int j=1;j<=m;j++)
	   {
	   	    if(mp[i][j]!='0' && st[i][j]==0 ){
	   	    	cnt++;//出现了一块新的未染过色的细胞区域
				bfs(i,j);//广度优先搜索:从i,j这个位置开始染色 
            }
		} 
	cout<<cnt<<endl; 
	return 0;
}

相关推荐

  1. 1329:【8.2】细胞

    2024-07-21 17:08:04       47 阅读
  2. 广度优先搜索

    2024-07-21 17:08:04       48 阅读
  3. bfs广度优先搜索

    2024-07-21 17:08:04       43 阅读
  4. BFS————广度优先搜索

    2024-07-21 17:08:04       44 阅读
  5. hnust 1966: 广度优先搜索

    2024-07-21 17:08:04       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 17:08:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 17:08:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 17:08:04       45 阅读
  4. Python语言-面向对象

    2024-07-21 17:08:04       55 阅读

热门阅读

  1. 工具篇(开发利器)

    2024-07-21 17:08:04       19 阅读
  2. 基于centos2009搭建openstack-t版-ovs网络-脚本运行

    2024-07-21 17:08:04       16 阅读
  3. 手写简易版Spring IOC容器04【学习】

    2024-07-21 17:08:04       19 阅读
  4. 网络文件传输

    2024-07-21 17:08:04       19 阅读
  5. vue2获取视频时长

    2024-07-21 17:08:04       19 阅读
  6. mybatis中的useGeneratedKeys和keyProperty

    2024-07-21 17:08:04       19 阅读
  7. AI Agent的创新之路:AutoGen与LangGraph的比较

    2024-07-21 17:08:04       14 阅读
  8. Android笔试面试题AI答之Activity(3)

    2024-07-21 17:08:04       15 阅读
  9. 关闭终端后继续执行celery任务

    2024-07-21 17:08:04       16 阅读
  10. 学习C语言之 深入了解数据存储

    2024-07-21 17:08:04       16 阅读
  11. WordPress杂技

    2024-07-21 17:08:04       19 阅读
  12. 赞扬的10条原则

    2024-07-21 17:08:04       19 阅读