广搜题目练习(c++)

题目

走出迷宫的最小步数

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,v;
	node(){};
	node(int aaaaa,int bbbbb,int ccccc)
	{
		x = aaaaa;
		y = bbbbb;
		v = ccccc;
	}
};queue<node> que;

char a[1010][1010];
int n,m;
int b[1010][1010];
int cnt;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

void bfs(int,int);

int main()
{
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	
	bfs(1,1);
	
	cout<<cnt;
	
	return 0;
}
void bfs(int _x,int _y)
{
	b[_x][_y] = 1;
	que.push({_x,_y,1});
	while(que.empty()==false)
	{
		node head = que.front();
		que.pop();
		for(int i = 0;i<4;i++)
		{
			int tx = head.x+dx[i];
			int ty = head.y+dy[i];
			if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&b[tx][ty]==0&&a[tx][ty]=='.')
			{
				b[tx][ty] = 1;
				que.push({tx,ty,head.v+1});
				if(tx==n&&ty==m)
				{
					cnt = que.back().v;
					return;
				}
			}
		}
	}
}

走出迷宫的最短路径

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,v;
	node(){};
	node(int aaaaa,int bbbbb,int ccccc)
	{
		x = aaaaa;
		y = bbbbb;
		v = ccccc;
	}
};queue<node> que;

char a[1010][1010];
int n,m;
int b[1010][1010];
int cnt;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

void bfs(int,int);

int main()
{
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	cnt = -1;
	bfs(1,1);
	if(cnt==-1) cout<<"no way";
	else cout<<cnt;
	
	return 0;
}
void bfs(int _x,int _y)
{
	b[_x][_y] = 1;
	que.push({_x,_y,1});
	while(que.empty()==false)
	{
		node head = que.front();
		que.pop();
		for(int i = 0;i<4;i++)
		{
			int tx = head.x+dx[i];
			int ty = head.y+dy[i];
			if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&b[tx][ty]==0&&a[tx][ty]=='.')
			{
				b[tx][ty] = 1;
				que.push({tx,ty,head.v+1});
				if(tx==n&&ty==m)
				{
					cnt = que.back().v;
					return;
				}
			}
		}
	}
}

迷宫出口

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,v;
	node(){};
	node(int aaaaa,int bbbbb,int ccccc)
	{
		x = aaaaa;
		y = bbbbb;
		v = ccccc;
	}
};queue<node> que;

int a[1010][1010];
int n,m;
int b[1010][1010];
int sx,sy,ex,ey;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

bool bfs(int,int);

int main()
{
	cin>>n;
	m = n;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	cin>>sx>>sy>>ex>>ey;
	if(a[sx][sy]==1||a[ex][ey]==1)
	{
		cout<<"NO";
		return 0;
	}
	if(bfs(sx,sy)==true) cout<<"YES";
	else cout<<"NO";
	
	
	return 0;
}
bool bfs(int _x,int _y)
{
	b[_x][_y] = 1;
	que.push({_x,_y,1});
	while(que.empty()==false)
	{
		node head = que.front();
		que.pop();
		for(int i = 0;i<4;i++)
		{
			int tx = head.x+dx[i];
			int ty = head.y+dy[i];
			if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&b[tx][ty]==0&&a[tx][ty]==0)
			{
				b[tx][ty] = 1;
				que.push({tx,ty,head.v+1});
				if(tx==ex&&ty==ey)
				{
					return true;
				}
			}
		}
	}
	return false;
}

鸡飞狗不跳

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,v;
	node(){};
	node(int aaaaa,int bbbbb)
	{
		x = aaaaa;
		v = bbbbb;
	}
};queue<node> que;

int n,m;
int a[100010];

int bfs();

int main()
{
	
	cin>>n>>m;
	if(n==m) cout<<0;
	else cout<<bfs();
	
	return 0;
}
int bfs()
{
	a[n] = 1;
	que.push({n,0});
	while(que.empty()==false)
	{
		node q1 = {que.front().x+1,que.front().v+1};
		node q2 = {que.front().x-1,que.front().v+1};
		node q3 = {que.front().x*2,que.front().v+1};
		if(q1.x==m||q2.x==m||q3.x==m) return q1.v;
		if(q1.x>=0&&q1.x<=100010&&a[q1.x]==0)
		{
			que.push(q1);
			a[q1.x] = 1;
		}
		if(q2.x>=0&&q2.x<=100010&&a[q2.x]==0)
		{
			que.push(q2);
			a[q2.x] = 1;
		}
		if(q3.x>=0&&q3.x<=100010&&a[q3.x]==0)
		{
			que.push(q3);
			a[q3.x] = 1;
		}
		que.pop();
	}
	
}
相关推荐
没落之王10 分钟前
易学使者郑氏正脉郑冰推演马航事件
大数据·人工智能·算法·机器学习·可用性测试
阿部多瑞 ABU28 分钟前
告别手工核图:基于 .NET + MuPDFCore 的 CAD 等轴测 PDF 材料表提取与新旧版本对比实战
后端·算法·ui·pdf·c#
郝学胜-神的一滴32 分钟前
并查集深度入门:从玄学抽象到 QuickFind & QuickUnion 源码实战
数据结构·c++·python·程序人生·算法·软件开发
比奇堡裤头村33 分钟前
统计学习方法——决策树
算法·决策树·学习方法
灵晔君1 小时前
【Linux】进程控制(一)——进程创建、进程终止与进程等待
linux·c语言·算法
ShineWinsu9 小时前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
数模竞赛Paid answer10 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆10 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
Lzg_na10 小时前
constexpr的优势
c++
想吃火锅100512 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展