2019.4.27 提高B组 T1 nssl-1316 血色先锋队

版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/89600493

D e s c r i p t i o n Description

给定一张图和一些点,如果每个点能以每小时一格的速度向四周扩散,问每个给出的点什么时候被感染

数据范围: n , m 500 , 50 0 2 n,m\leq 500,点数\leq 500^2


S o l u t i o n Solution

把每个点入队,然后宽搜就行了

时间复杂度: O ( n m ) O(nm)


C o d e Code

#include<queue>
#include<cstdio>
#include<cctype>
#define LL long long
using namespace std;int n,m,A,B,map[501][501],ans[250001];
bool ok[501][501];
struct node{int x,y,h;};
queue<node>q;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
inline LL read()
{
    char c;int f=0,d=1;
    while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
    while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
    return d*f;
}

signed main()
{
	n=read();m=read();A=read();B=read();
	for(register int i=1,x,y;i<=A;i++) ok[x=read()][y=read()]=1,q.push((node){x,y,0});
	for(register int i=1;i<=B;i++) map[read()][read()]=i;
	while(q.size())
	{
		node from=q.front();q.pop();
		for(register int i=0;i<4;i++)
		{
			node to=(node){from.x+dx[i],from.y+dy[i],from.h+1};
			if(to.x<1||to.x>n||to.y<1||to.y>m||ok[to.x][to.y]) continue;
			ans[map[to.x][to.y]]=to.h;
			q.push(to);ok[to.x][to.y]=true;
		} 
	}
	for(register int i=1;i<=B;i++) printf("%d\n",ans[i]);
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/89600493