广搜求条
查看原帖
广搜求条
1271316
违规用户名K&xs3Z^楼主2024/8/20 09:00
#include<bits/stdc++.h>
using namespace std;
queue<int> x;
queue<int> y;
char a[1110][1110];
int step=-1,n,s1,t1,s2,t2;
bool vis[1110][1110];
int fx[6]={0,0,1,0,-1};
int fy[6]={0,1,0,-1,0};
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>a[i][j];
	cin>>s1>>t1>>s2>>t2;
	x.push(s1),y.push(t1);
	while(!x.empty()){
		for(int i=1;i<=4;i++){
		int tx=x.front()+fx[i];
		int ty=y.front()+fy[i];
		if(a[tx][ty]=='0'&&!vis[tx][ty]){
			vis[tx][ty]=1;
			a[tx][ty]='1';
			step++;
			x.push(tx),y.push(ty);
			if(tx==s2&&ty==t2){
				cout<<step<<endl;
				return 0;
			}
		}
		}
		x.pop(),y.pop();
	}
	return 0;
}


2024/8/20 09:00
加载中...