不知道哪里错了,人机求助
查看原帖
不知道哪里错了,人机求助
1099418
caojiyao楼主2024/12/3 20:56
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
bool b;
char c[105][105];
queue<int> que,que1;

inline void bfs(int x,int y){
	if(x==n-1&&y==m-1){
		cout<<"Yes";
		b=1;
		return ;
	}
	if(x>n-1||y>m-1||x<0||y<0)return ;
	if(c[x][y]=='#')return ;
	else{
		c[x][y]='#';
		if(x-1>=0&&x-1<=n-1&&y>=0&&y<=m-1&&c[x-1][y]!='#'){
			que.push(x-1);
			que1.push(y);
		}
		if(x+1>=0&&x+1<=n-1&&y>=0&&y<=m-1&&c[x+1][y]!='#'){
			que.push(x+1);
			que1.push(y);
		}
		if(x>=0&&x<=n-1&&y-1>=0&&y-1<=m-1&&c[x][y-1]!='#'){
			que.push(x);
			que1.push(y-1);
		}
		if(x>=0&&x<=n-1&&y+1>=0&&y+1<=m-1&&c[x][y+1]!='#'){
			que.push(x);
			que1.push(y+1);
		}

		int a=que.front(),d=que1.front();
		que.pop();que1.pop();
		bfs(a,d);
		
	}
	if(que.empty())return ;
}

int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>c[i][j];
		}
	}

	bfs(0,0);
	if(b==0){
		cout<<"No";
	}
	
	
	return 0;
}
2024/12/3 20:56
加载中...