栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

广度优先搜索BFS

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

广度优先搜索BFS

队列queue

●只允许对前端(front)队首进行删除操作

●对后端(rear)队尾进行插入操作

入队(push)

出队(pop)

判断队列是否为空(empty)

统计队列元素个数(size)

访问队首元素(front)

用队列输出元素:

#include
#include
#include
using namespace std;
int main()
{
	queueq;
	q.push("zhangshan");
	q.push("lisi");
	q.push("wangwu");
	while(!q.empty()){
		cout< 

报数游戏:

 

 

广度优先搜索

 

 

 

 

迷宫游戏最短路

 

 

 

#include
#include
#include
using namespace std;
int n,m;
string maze[110];
bool vis[110][110];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
bool in(int x,int y){
		return 0<=x&&xq;
	q.push(node(sx,sy,0));
	vis[sx][sy]=true;
	while(!q.empty()){
		node now=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int tx=now.x+dir[i][0];
			int ty=now.y+dir[i][1];
			if(in(tx,ty)&&maze[tx][ty]!='*'&&!vis[tx][ty])
				if(maze[tx][ty]=='T'){
					return now.d+1;
				}else{
					vis[tx][ty]=true;
					q.push(node(tx,ty,now.d+1));
				}
			}
		}
	
	return -1;
}
int main(){
	cin>>n>>m;
	for(int i=0;i>maze[i];
	}
	int x,y;
	for(int i=0;i 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784410.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号