我写的BFS死循环了qwq
查看原帖
我写的BFS死循环了qwq
661595
a2lyaXNhbWUgbWFyaXNh楼主2022/10/7 12:02

求调QAQ

#include<bits/stdc++.h>
using namespace std;
int sx,sy,ex,ey,n,m;
bool g[2010][2010];
bool vis[2010][2010];
int step[2010][2010];
char tmp;
void bfs();
short dx[]=
			{1,0,-1,0},
	  dy[]=
	  		{0,-1,0,1};
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			cin>>tmp;
			if(tmp=='#')g[i][j]=1;
			else if(tmp=='d')ex=i,ey=j;
			else if(tmp=='m')sx=i,sy=j;
		}
	bfs();
	if(step[ex][ey])cout<<step[ex][ey];
	else cout<<"No Way!";
	return 0;
}
void bfs(){
	queue<pair<int,int> >q;
	q.push(make_pair(sx,sy));
	vis[sx][sy]=1;
	step[sx][sy]=0;
	while(q.size()){
		int x=q.front().first;
		int y=q.front().second;
		for(int i=0;i<4;i++){
			int xx=
					x+dx[i],
				yy=
					y+dy[i];
			if(xx>=1 and xx<=n and yy>=1 and yy<=0 and
				not vis[xx][yy] and not g[xx][yy]){
				step[xx][yy]=step[x][y]+1;
				q.push(make_pair(xx,yy));
				if(xx==ex and yy==ey)
					return;
			}	
		}
	}
}

马蜂奇怪勿喷

2022/10/7 12:02
加载中...