80分,求大佬指点
查看原帖
80分,求大佬指点
1368016
jackliya21楼主2024/10/25 20:31

代码如下

#include <bits/stdc++.h>
using namespace std;
int n,a1,b1,a2,b2;
bool mapp[1000][1000];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
struct node
{
	int x,y,step;
};
queue <node> q;
void bfs(int a,int b,int aa,int bb)
{
	node t;
	t.x = a;
	t.y = b;
	t.step = 0;
	q.push(t);
	mapp[a][b]=1;
	while (!q.empty())
	{
		t = q.front();
		q.pop();
		for (int i=0;i<4;i++)
		{
			int nx = t.x+dx[i];
			int ny = t.y+dy[i];
			if (nx==aa&&ny==bb)
			{
				cout << t.step+1;
				return;
			}
			if (!mapp[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<n)
			{
				node tt;
				tt.step = t.step+1;
				tt.x = nx;
				tt.y = ny;
				mapp[nx][ny] = 1;
				q.push(tt);
			}
		}
	}
}
int main()
{
	cin >> n;
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			scanf("%1d",&mapp[i][j]);
		}
	}
	cin >> a1 >> b1 >> a2 >> b2;
	if (a1==a2&&b1==b2)
	{
		cout << 0;
		return 0; 
	}
	bfs(a1,b1,a2,b2);
}
2024/10/25 20:31
加载中...