输出呢()
查看原帖
输出呢()
955954
Miracle_InDream楼主2024/1/2 22:21
#include<bits/stdc++.h>
using namespace std;
int n;
char mp[1005][1005];
int vis[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int a,b,c,d;
struct node
{
	int x;
	int y;
	int step;
};
node s,t;
queue<node> q;
void bfs()
{
	s.x=a;
	s.y=b;
	t.x=c;
	t.y=d;
	s.step=0;
	q.push(s);
	while(!q.empty())
	{
		node tmp=q.front();
		q.pop();
		vis[tmp.x][tmp.y]=1;
		if(tmp.x==c&&tmp.y==d)
		{
			cout<<tmp.step;
			return;
		}
		for(int i=0;i<4;i++)
		{
			node a;
			a.x=tmp.x+dx[i];
			a.y=tmp.y+dy[i];
			a.step=tmp.step+1;
			if(a.x>=0&&a.y>=0&&a.x<n&&a.y<n&&vis[a.x][a.y]==0&&mp[a.x][a.y]==0)
			{
				q.push(a);
			}
		}
	}
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>mp[i][j];
		}
	}
	cin>>a>>b>>c>>d;
	bfs();
	return 0;
}
2024/1/2 22:21
加载中...