跳到正文

个人记录

1.26-寒假集训-2【广度优先搜索】

Void_Rift约 14 分钟
原文
文章信息与版本

文章信息

收录
2026年1月27日
更新
2026年1月27日
篇幅
约 14 分钟

版本

  1. v1当前

广度优先搜索(BFS)(BFS)

-T725354 仙岛求药-

思路

  • 模板题(1/1)
  • 注: 由于有多组测试样例,所以每次 BFSBFS 后需要清空(输出要换行)!!!!!!

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10;
struct node
{
	int x,y,step;
};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int n,m,sx,sy,ex,ey;
char c[N][N];
bool vis[N][N];
void bfs()
{
	queue<node> q;
	q.push({sx,sy,0ll});
	vis[sx][sy]=true;
	while(!q.empty())
	{
		node f=q.front();
		q.pop();
		if(f.x==ex && f.y==ey)
		{
			cout<<f.step<<"\n";
			return ;
		}
		for(int i=0;i<4;i++)
		{
			int x=f.x+dx[i];
			int y=f.y+dy[i];
			int step=f.step+1;
			if(x>=1 && x<=n && y>=1 && y<=m && !vis[x][y] && c[x][y]!='#')
			{
				vis[x][y]=true;
				q.push({x,y,step});
			}
		}
	}
	cout<<"-1\n";
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	while(true)
	{
		memset(vis,false,sizeof(vis));
		cin>>n>>m;
		if(n==0 && m==0)
		{
			break;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>c[i][j];
				if(c[i][j]=='@')
				{
					sx=i;
					sy=j;
				}
				if(c[i][j]=='*')
				{
					ex=i;
					ey=j;
				}
			}
		}
		bfs();
	}
	return 0;
}

-P2873 [USACO07DEC] Mud Puddles S-

思路

  • 模板题(2/1)
  • 这道题由于有负数(−500(-500 ~ 500)500),所以需要把所有的点的坐标加上 500500(包括起点和终点)

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10;
struct node
{
	int x,y,step;
};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,ex,ey,a[N][N];
bool vis[N][N];
void bfs()
{
	queue<node> q;
	q.push({500ll,500ll,0ll});
	vis[500][500]=true;
	while(!q.empty())
	{
		node f=q.front();
		q.pop();
		if(f.x==ex && f.y==ey)
		{
			cout<<f.step<<"\n";
			return ;
		}
		for(int i=0;i<4;i++)
		{
			int xx=f.x+dx[i];
			int yy=f.y+dy[i];
			int step=f.step+1;
			if(xx>=0 && xx<=1000 && yy>=0 && yy<=1000 && !vis[xx][yy] && a[xx][yy]!=1)
			{
				vis[xx][yy]=true;
				q.push({xx,yy,step});
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	memset(a,0,sizeof(a));
	memset(vis,false,sizeof(vis));
	cin>>ex>>ey>>n;
	ex+=500;
	ey+=500;
	while(n--)
	{
		int x,y;
		cin>>x>>y;
		x+=500;
		y+=500;
		a[x][y]=1;
	}
	bfs();
	return 0;
}

-T725348 走出迷宫的最短路径-

思路

  • bfs4
  • 用一个二维数组储存路径,在到达终点时递归输出路径即可(记得还有no way\text{no way})

由于copy第一题判断合法点的条件忘记转int而痛失100pts的屑

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10;
struct node
{
	int x,y;
}ans[N][N];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int n,m,sx,sy,ex,ey,a[N][N];
bool vis[N][N];
bool check(int x,int y)
{
	return (x>=1 && x<=n && y>=1 && y<=m && vis[x][y]==false && a[x][y]!=1);
}
void out(int x,int y)
{
	if(ans[x][y].x==-1 && ans[x][y].y==-1)
	{
		cout<<"("<<x<<","<<y<<")";
		return ;
	}
	out(ans[x][y].x,ans[x][y].y);
	cout<<"->("<<x<<","<<y<<")";
}
void bfs()
{
	queue<node> q;
	q.push({sx,sy});
	vis[sx][sy]=true;
	ans[sx][sy]={-1,-1};
	while(!q.empty())
	{
		node f=q.front();
		q.pop();
		if(f.x==ex && f.y==ey)
		{
			out(ex,ey);
			return ;
		}
		for(int i=0;i<4;i++)
		{
			int x=f.x+dx[i];
			int y=f.y+dy[i];
			if(check(x,y))
			{
				vis[x][y]=true;
				ans[x][y]={f.x,f.y};
				q.push({x,y});
			}
		}
	}
	cout<<"no way";
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	memset(vis,false,sizeof(vis));
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	cin>>sx>>sy>>ex>>ey;
	bfs();
	return 0;
}

-P2960 [USACO09OCT] Invasion of the Milkweed G-

思路

  • 坐 标 是 反 的😱(反向输入即可)
  • 八 方 向😱(扩展方向数组即可)
  • 每次 BFSBFS 时更新最大周数,BFSBFS 结束时输出即可

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10;
struct node
{
	int x,y,step;
};
int dx[8]={-1,-1,1,1,0,-1,1,0};
int dy[8]={-1,1,-1,1,-1,0,0,1};
int n,m,sx,sy,maxn=0;
char c[N][N];
bool vis[N][N];
void bfs()
{
	queue<node> q;
	q.push({sx,sy,0ll});
	vis[sx][sy]=true;
	while(!q.empty())
	{
		node f=q.front();
		q.pop();
		for(int i=0;i<8;i++)
		{
			int x=f.x+dx[i];
			int y=f.y+dy[i];
			int step=f.step+1;
			if(x>=1 && x<=n && y>=1 && y<=m && !vis[x][y] && c[x][y]!='*')
			{
				vis[x][y]=true;
				maxn=max(maxn,step);
				q.push({x,y,step});
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	memset(vis,false,sizeof(vis));
	cin>>m>>n>>sy>>sx;
	for(int i=n;i>=1;i--)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>c[i][j];
		}
	}
	bfs();
	cout<<maxn;
	return 0;
}

-P10487 Nightmare II-

思路

  • 对小erriyueerriyue和女友分别做 BFSBFS,记录两人到达每个格子的时间;然后通过黑曼巴曼哈顿距离判断格子再 tt 时是否被鬼魂占领(距离 ≤2t\space\le2t),并在移动时避开危险区域;最后遍历所有格子,取两人都能到达的格子中maxmax(小erriyueerriyue时间,女友时间)的最小值,即为最短相遇时间,无则输出 −1-1

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+10,inf=0x3f3f3f3f;
struct node
{
	int x,y;
};
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int T,n,m,sx_m,sy_m,sx_g,sy_g,dis[2][N][N];
char c[N][N];
bool vis[N][N];
vector<pair<int,int> > v;
bool check(int x,int y,int t)
{
	for(int i=0;i<=1;i++)
	{
		if(abs(v[i].first-x)+abs(v[i].second-y)<=2*t)
		{
			return true;
		}
	}
	return false;
}
void bfs(int sx,int sy,int k)
{
	memset(vis,false,sizeof(vis));
	queue<node> q;
	q.push({sx,sy});
	dis[k][sx][sy]=0;
	vis[sx][sy]=true;
	int times=0;
	while(!q.empty())
	{
		times++;
		int cnt=(k==1 ? 1 : 3);
		while(cnt--)
		{
			int len=q.size();
			while(len--)
			{
				node f=q.front();
				q.pop();
				for(int i=0;i<4;i++)
				{
					int x=f.x+dx[i];
					int y=f.y+dy[i];
					if(!check(f.x,f.y,times) && x>=1 && x<=n && y>=1 && y<=m && !vis[x][y] && c[x][y]!='X')
					{
						dis[k][x][y]=times;
						vis[x][y]=true;
						q.push({x,y});
					}
				}
			}
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>c[i][j];
				if(c[i][j]=='M')
				{
					sx_m=i;
					sy_m=j;
				}
				if(c[i][j]=='G')
				{
					sx_g=i;
					sy_g=j;
				}
				if(c[i][j]=='Z')
				{
					v.push_back({i,j});
				}
			}
		}
		memset(dis,-1,sizeof(dis));
		bfs(sx_m,sy_m,0);
		bfs(sx_g,sy_g,1);
		int minn=inf;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(dis[0][i][j]!=-1 && dis[1][i][j]!=-1)
				{
					minn=min(minn,max(dis[0][i][j],dis[1][i][j]));
				}
			}
		}
		cout<<(minn!=inf ? minn : -1)<<"\n";
		v.clear();
	}
	return 0;
}
原文