宽度优先搜索bfs经典习题+代码

离开中山路

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e3+5;
int stx,sty,edx,edy;
int dx[N] = {0,0,1,-1};
int dy[N] = {1,-1,0,0};
int dist[N][N];
bool vis[N][N];
int n;
char va[N][N];
void bfs()
{
	queue<pair<int,int> > q;
	vis[stx][sty] = 1;
	dist[stx][sty] = 0;
	q.push({stx,sty});
	while(q.size()>=1)
	{
		auto now = 	q.front();//auto使now和queue同类型是pair类型能存两个值 
		q.pop();
		int x = now.fi,y = now.se;
		for(int i =0;i<=3;i++)
		{
			int xx = x+dx[i],yy = y+dy[i];
			if(xx>=1&&xx<=n&&y>=1&&y<=n&&vis[xx][yy]==0&&va[xx][yy]=='0')
			{
				//用(x,y)去更新(xx,yy) 
				vis[xx][yy]= 1;             //口诀:一标,二变,三存放。
				dist[xx][yy] = dist[x][y]+1;
				q.push({xx,yy});
			}
		}
	}
}
int main()
{
	IOS;
	cin>>n;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			cin>>va[i][j];
		}
	}
	cin>>stx>>sty>>edx>>edy;
	bfs();
	cout<<dist[edx][edy];
	return 0;
	
}

奇怪的电梯

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e3+5;
int n,sta,edb;
int va[N];
int dist[N];
int vis[N];
void bfs()
{
	queue<int> q;//定 
	dist[sta] = 0;//时 
	vis[sta] = 1;//标 
	q.push(sta);//放 
	while(q.size()>=1)
	{
		auto now = q.front();
		q.pop();
		int up = now+va[now];
		int down = now-va[now];
		if(up>=1&&up<=n&&vis[up] ==0)
		{
			vis[up] = 1;
			dist[up] = dist[now]+1;
			q.push(up);
		}
		if(down>=1&&down<=n&&vis[down] ==0)
		{
			vis[down] = 1;
			dist[down] = dist[now]+1;	
			q.push(down);
		}
	}
}
int main()
{
	IOS;
	cin>>n>>sta>>edb;
	for(int i = 1;i<=n;i++) cin>>va[i];
	bfs();
	if(vis[edb] !=0)cout<<dist[edb];
	else cout<<-1;
}

马的遍历

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e3+5;
int n,m,stx,sty;

bool vis[N][N];
int dist[N][N];
int dx[N] ={-2,-1,1,2,-2,-1,1,2};
int dy[N] ={1,2,2,1,-1,-2,-2,-1};
void bfs()
{
	queue<pair<int,int> > q;
	vis[stx][sty] = 1;
	dist[stx][sty] = 0;
	q.push({stx,sty});
	while(q.size())
	{
		auto now = q.front();
		int x = now.fi,y = now.se;
		q.pop();
		for(int i=0;i<8;i++)
		{
			int xx = x+dx[i],yy=y+dy[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy] == 0)
			{
				vis[xx][yy] = 1;
				dist[xx][yy] = dist[x][y]+1;
				q.push({xx,yy});
	 		}
		}
	
	}
}
int main()
{
	//IOS;
	cin>>n>>m>>stx>>sty;
	bfs();
for(int i = 1;i<=n;i++)
{
	for(int j=1;j<=m;j++)
	{
		if(vis[i][j]==0) printf("%-5d",-1);
		else printf("%-5d",dist[i][j]);
	}
	cout<<"\n";
}
}

Lake Counting S数水坑

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e4+5;
int n,m;
int cnt = 0;
char va[N][N];
int dx[N] ={0,0,1,-1,-1,1,1,-1};
int dy[N] ={1,-1,1,-1,1,-1,0,0};
void bfs(int a,int b)
{
	queue< pair<int,int> > q;
	q.push({a,b});
	while(q.size())
	{
		auto now = q.front();
		int x = now.fi,y = now.se;
		q.pop();
		for(int k = 0;k<=7;k++)
		{
			int xx = x+dx[k],yy=y+dy[k];
			if(va[xx][yy] =='W')
			{
				va[xx][yy]='.';
				q.push({xx,yy});
			}
		}
	}
}
int main()
{
	IOS;
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cin>>va[i][j];
		}
	}
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			if(va[i][j]=='W')
			{
				cnt++;
				bfs(i,j);	
			}
		}
	}
	cout<<cnt;
}

观星

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1505;
int n,m;
int maxn = 0;
map<int,int> mp;
bool vis[N];
char va[N][N];
int dx[N] ={0,0,1,-1,-1,1,1,-1};
int dy[N] ={1,-1,1,-1,1,-1,0,0};
void bfs(int a,int b)
{
	queue< pair<int,int> > q;
	q.push({a,b});
	va[a][b] = '.';
	int sum = 0;
	while(q.size())
	{
		auto now = q.front();
		int x = now.fi,y = now.se;
		q.pop();
		sum++;
		for(int k = 0;k<=7;k++)
		{
			int xx = x+dx[k],yy=y+dy[k];
			if(va[xx][yy] =='*')
			{
				va[xx][yy]='.';
				q.push({xx,yy});
			}
		}
	}
	mp[sum]++;
}
int main()
{
	IOS;
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			cin>>va[i][j];
		}
	}
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=m;j++)
		{
			if(va[i][j]=='*')
			{
				bfs(i,j);
			}
		}
	}                //fi是星星个数,se是星座个数 
	for(auto t:mp) maxn = max(maxn,t.se*t.fi);
	cout<<mp.size()<<" "<<maxn;//mp.size()为mp中共有多少种
	                           //如有mp[4] = 2,mp[3] = 1,mp[2]=1,
							   //则mp.size()= 3  
}

填涂颜色

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e3+5;
int va[N][N];
bool vis[N][N];
int n;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0} ;
void bfs()
{
	queue<pair<int,int> > q;
	vis[0][0] = 1;
	q.push({0,0});
	while(q.size())
	{
		auto now = q.front();
		int x = now.fi,y = now.se;
		q.pop();
		for(int i = 0;i<=3;i++)
		{
			int xx = x+dx[i],yy = y+dy[i];
			if(xx>=0&&xx<=n+1&&yy>=0&&yy<=n+1&&vis[xx][yy]==0&&va[xx][yy]==0)
			{
				vis[xx][yy]=1;
				q.push({xx,yy});
			}
		}
	}
}
int main()
{
	IOS;
	cin>>n;
	for(int i = 1;i<=n;i++) 
	{
		for(int j = 1;j<=n;j++)
		{
			cin>>va[i][j];
		}
	}
	bfs();
	for(int i = 1;i<=n;i++)
	{
		for(int j = 1;j<=n;j++)
		{
			if(va[i][j]==0&&vis[i][j]==0)
			{
				cout<<2<<" ";
			}
			else cout<<va[i][j]<<' ';
		}
		cout<<"\n";
	}
}
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值