请搜索大佬来帮忙看看,求求了...
查看原帖
请搜索大佬来帮忙看看,求求了...
1147487
leonl_l楼主2024/6/10 17:38

为练习广搜,这道题我用bfs做:

#include<bits/stdc++.h>
using namespace std;
char gaoqiao='s',fish='g',road='.',wall='#',a[10005][10005];
int n,m,xx,yy,ex,ey;
int vis[10006][10006];
int dx[5] = {1,-1,0,0};
int dy[5] = {0,0,1,-1};
int q[10006][3];
bool flag=false;
void xray(){
	for (int i = 1;i <= n;i++){
    	for (int j = 1;j <= m;j++){
    		if(a[i][j]==gaoqiao){
    			xx=i;
    			yy=j;
    		}
    		if(a[i][j]==fish){
    			ex=i;
    			ey=j;
    		}
    	}
    }
}
void bfs(){
	int head=1,tail=1;
	q[1][1]=xx;
	q[1][2]=yy;
	while(head<=tail){
		for(int i=0;i<4;i++){
			int tx=dx[i]+q[head][1];
			int ty=dy[i]+q[head][2];
			if(tx==ex and ty==ey){
				cout<<"Yes"<<endl;
				flag=true;
				exit(0);
			}
			if(a[tx][ty]==road and !vis[tx][ty] and tx>0 and tx<=n and ty>0 and ty<=m){
				vis[tx][ty]=1;
				tail++;
				q[tail][1]=tx;
				q[tail][2]=ty;
			}
		}
		head++;
	}
} 
int main(){
	cin>>n>>m;
    for (int i = 1;i <= n;i++){
    	for (int j = 1;j <= m;j++) cin>>a[i][j];
    }
	xray();   
	bfs();
	if(flag==false){
        cout<<"No"<<endl;
    }
	return 0;
} 

样例全过,自己造的图也过,可是WA,卡在#18了,求各位大佬指点qvq

2024/6/10 17:38
加载中...