70分求助
查看原帖
70分求助
999767
dasigouride楼主2023/12/24 14:52
#include<bits/stdc++.h>
using namespace std;
bool vis[505][505];
struct node
{
	int x,y,s;
};
int table[505][505];
int n,m,d,j,stx,sty;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
bool check(int x,int y)
{
	return x>=1&x<=n&&y>=1&&y<=m&&!vis[x][y];
}
void bfs()
{
	queue<node> q;
	q.push({stx,sty,0});
	vis[stx][sty]=true;
	table[stx][sty]=0;
	while(!q.empty())
	{
		node t=q.front();
		q.pop();
		table[t.x][t.y]=min(table[t.x][t.y],t.s);
		for(int i=-0;i<4;i++)
		{
			int tx=t.x+dx[i];
			int ty=t.y+dy[i];
			if(check(tx,ty))
			{
				vis[tx][ty]=true;
				q.push({tx,ty,t.s+1});
			}
		}
	}
}
int main()
{
	cin>>n>>m>>d>>j;
	int x,y;
	memset(table,0x3f3f3f,sizeof table);
	for(int i=1;i<=d;i++)
	{
		memset(vis,false,sizeof vis);
		cin>>x>>y;
		stx=x,sty=y;
		bfs();
	}
	for(int i=1;i<=j;i++)
	{
		cin>>x>>y;
		cout<<table[x][y];
		puts("");
	}
	return 0;
}
2023/12/24 14:52
加载中...