bfs求调
查看原帖
bfs求调
1079493
Hy13_xsm楼主2024/8/29 15:12

学的时候bfs没认真听,导致写不出bfs。RE了3个点,帮我下。

#include<bits/stdc++.h>
using namespace std;
int n,m,f;
char c[2005][2005];
bool vis[2005][2005];
struct node{
	int x,y;
};
int dx[]={0,0,0,1,-1},dy[]={0,1,-1,0,0};
void bfs()
{
	queue<node>q;
	q.push({1,1});
	while(!q.empty())
	{
		while(vis[q.front().x][q.front().y])q.pop();
		if(q.empty())return;
		int x=q.front().x,y=q.front().y;
		q.pop();
		vis[x][y]=1;
		for(int i=1;i<=4;i++)
		{
			int tx=x+dx[i],ty=y+dy[i];
			if(c[tx][ty]=='.')q.push({tx,ty});
			if(tx==n&&ty==m){
				f=1;
				return;
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		cin>>c[i][j];
	}
	bfs();
	if(f)cout<<"Yes";
	else cout<<"No";
	return 0;
}
2024/8/29 15:12
加载中...