救民于水火 ans 营救

x-hechengye 2022-05-21 15:56:47 36 返回题目

#include <bits/stdc++.h>
using namespace std;
char a[1010][1010];
int x,y,u,v,n,dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
struct node{
	int x,y,d;
};
queue<node>q;
int bfs(int x,int y){
	q.push(node{x,y,0});
	a[x][y]='1';
	while(!q.empty()){
		node now=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int nx=now.x+dx[i];
			int ny=now.y+dy[i];
			if(a[nx][ny]=='1') continue;
			if(nx==u && ny==v) return now.d+1;
			q.push(node{nx,ny,now.d+1});
			a[nx][ny]='1';
		}
	}
}
int main(){
	cin>>n;
	memset(a,'1',sizeof(a));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) 
		    cin>>a[i][j];
	cin>>x>>y>>u>>v;
	cout<<bfs(x,y);
	return 0;
}
{{ vote && vote.total.up }}