P1332,nssl1316-血色先锋军【bfs】

版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/89600859

正题

题目链接:https://www.luogu.org/problemnew/show/P1332


题目大意

对于每个领主求与最近的感染源的距离


解题思路

那么水还要我讲???


c o d e code

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=510;
int v[N][N],n,m,a,b;
queue<int> qx,qy;
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
void bfs()
{
	while(!qx.empty())
	{
		int x=qx.front(),y=qy.front();
		qx.pop();qy.pop();
		for(int k=0;k<4;k++){
			int zx=x+dx[k],zy=y+dy[k];
			if(x<=0||y<=0||x>n||y>m||v[zx][zy]) continue;
			v[zx][zy]=v[x][y]+1;
			qx.push(zx);qy.push(zy);
		}
	}
}
int main()
{
	scanf("%d%d%d%d",&n,&m,&a,&b);
	for(int i=1;i<=a;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		qx.push(x);qy.push(y);
		v[x][y]=1;
	}
	bfs();
	for(int i=1;i<=b;i++)
	{
		int x,y; 
		scanf("%d%d",&x,&y);
		printf("%d\n",v[x][y]-1);
	}
} 

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/89600859