求助大佬
查看原帖
求助大佬
365433
Mark_M楼主2022/2/13 14:37

请看这代码问题出在哪

#include<bits/stdc++.h>
using namespace std;
char c[1005][1005];
bool vis[1005][1005];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0},};
int x_1,y_1,x_2,y_2;
int n;

struct Node{
	int x;
	int y;
	int step;
};

bool check(int x,int y){
	if(x<0||x>=n||y<0||y>=n||vis[x][y]==true||c[x][y]=='1'){
		return false;
	}
	return true;
}

int BFS(){
	queue<Node> Q;
	Node st={x_1,y_1,0};
	Q.push(st);
	vis[x_1][y_1]=true;
	
	while(!Q.empty()){
		Node p=Q.front();
		Q.pop();
		if(p.x==x_2&&p.y==y_2){
			return p.step;
		}
		for(int i=0;i<4;i++){
			int next_x=p.x+dir[i][0];
			int next_y=p.x+dir[i][1];
			if(check(next_x,next_y)){
				Node next_p={next_x,next_y,p.step+1};
				Q.push(next_p);
				vis[next_x][next_y]=true;
			}
		}
	}
}

int main(int argc, char** argv){
	memset(vis,false,sizeof(vis));
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>c[i][j];
		}
	}
	cin>>x_1>>y_1>>x_2>>y_2;
	
	int ans=BFS();
	cout<<ans;
	return 0;
}
2022/2/13 14:37
加载中...