求调
查看原帖
求调
759976
wflhx2011楼主2024/7/16 20:53
 #include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,gx,gy,f,dx[4]={0,1,-1,0},dy[4]={-1,0,0,1},vis[55][55];
char c[55][55];
struct nod
{
	int x,y,ans;
};
queue<nod> q;
int bfs(int xx,int yy)
{
	q.push((nod){xx,yy,0});
	vis[xx][yy]=1;
	while(!q.empty())
	{
		nod u=q.front();
		q.pop();
		if(u.x==gx&&u.y==gy)
			return f;
		for(int k=0;k<4;k++)
		{
			int ax=u.x+dx[k],ay=u.y+dy[k];
			f=u.ans+1;
			if(ax>=1&&ax<=n&&ay>=1&&ay<=m&&c[ax][ay]=='.'&&!vis[ax][ay])
				q.push((nod){ax,ay,f}),vis[ax][ay]=1;
		}
	}
}
int main()
{
	cin>>n>>m>>sx>>sy>>gx>>gy;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>c[i][j];
	cout<<bfs(sx,sy)<<endl;
	return 0;
}

   
2024/7/16 20:53
加载中...