社区讨论

请搜索大佬来帮忙看看,求求了...

AT_dfs_a深さ優先探索参与者 4已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lx8s89m5
此快照首次捕获于
2024/06/10 17:40
2 年前
此快照最后确认于
2024/06/10 20:42
2 年前
查看原帖
为练习广搜,这道题我用bfs做:
CPP
#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

回复

5 条回复,欢迎继续交流。

正在加载回复...