RE求调
查看原帖
RE求调
1017223
蒟蒻whker楼主2024/9/29 21:17
#include<iostream>
using namespace std;
int n,k,sx,sy,tx,ty,q[10005][3]={},f[4][2]={0,1 ,1,0 ,-1,0, 0,-1};
bool m[1005][1005]={}; 
int bfs(int x,int y)
{bool b=0;
	int head=0,tail=0;
	q[tail][0]=x;
	q[tail][1]=y;
	q[tail][2]=0;
	m[x][y]=0;
	tail++;
	while(head<tail)
	{
		if(q[head][0]==tx&&q[head][1]==ty)return q[head][2];
		for(int i=0;i<4;i++) 
		{
			int xx=q[head][0]+f[i][0],yy=q[head][1]+f[i][1];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&m[xx][yy])
			{
				q[tail][0]=xx;
				q[tail][1]=yy;
				q[tail][2]=q[head][2]+1;
				m[xx][yy]=0;
				tail++;
				b=1;
			}
		}
		head++; 
	}
}
int main(){
	//freopen("P1746_1.in","r",stdin);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			char c;cin>>c;
			if(c=='1')m[i][j]=0;
			else m[i][j]=1;
		}
	}cin>>sx>>sy>>tx>>ty;
	if(sx==tx&&sy==ty){cout<<"0\n";return 0;}
	cout<<bfs(sx,sy);
	return 0;
}
2024/9/29 21:17
加载中...