为什么运行时会炸
查看原帖
为什么运行时会炸
632063
shipeiqian楼主2022/7/16 21:01

rt,运行时就会卡住,然后返回3221225725

就是炸了啊

#include <iostream>
using namespace std;
char a[505][505];
int n,m,sx,sy,ex,ey;
int dx[4]={0,-1,1,0},dy[4]={-1,0,0,1};
bool vi[505][505],found=false;
bool check(int x,int y){
	if(x<=0||y<=0||x>n||y>m)return false;
	if(vi[x][y])return false;
	if(a[x][y]=='#')return false;
	return true;
}
void dfs(int x,int y){
	if(x==ex && y==ey){
		found=true;
		return ;
	}
	vi[x][y]==true;
	for(int i=0;i<4;i++){
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(check(nx,ny)){
			dfs(nx,ny);
		}
	}
	return ;
}
int main(){
	cin >>n >>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >>a[i][j];
			vi[i][j]==false;
			if(a[i][j]=='s'){sx=i,sy=j;}
			if(a[i][j]=='g'){ex=i,ey=j;}
		}
	}
	dfs(sx,sy);
	cout <<(found?"Yes":"No");
	return 0;
}

2022/7/16 21:01
加载中...