博客园 首页 私信博主 显示目录 隐藏目录 管理

Security Guards(bfs+预处理)Gym - 101954B

In the course of the last few weeks, Binary Casino has been afflicted by a local crime wave which is primarily focused on casinos in the neighborhood. Although there are surveillance cameras installed in Binary Casino, thieves usually manage to sneak out with relative ease as there is almost nobody patrolling in the casino.

After the theft of all Friday's earnings, the manager of Binary Casino has lost his patience and decided to reinforce security of the casino by hiring a vast number of security guards. However, nobody in the casino was capable of coming up with a plan on how to distribute guards across the whole casino to maximize security. Security guards are thus scattered across the casino in no systematic way. Fortunately, their locations can be described by integer coordinates in a 2D plane.

Because of the uneven distribution of security guards, in case of a reported robbery it is very hard for security supervisors to determine which guard is closest to the location of the incident. The task is even harder due to the constrained space in the casino which consists of endless aisles of slot machines. This limitation forces each guard to travel from one location to another in a sequence of steps. In each step, he/she can change each of his/her coordinates by 11, 00 or 1−1. The distance between two locations is equal to the minimum number of steps the guard has to do to get from one location to the other one.

The task is, for a given locations of guards and a set of locations of security incidents, to compute for each incident its smallest distance to any of the guards. This will allow security supervisors to alert appropriate guards and will greatly increase the casino security.

Input

The first line of input contains two integers NN and QQ (1N,Q31051≤N,Q≤3⋅105), the number of guards and the number of security incidents, respectively. After that, NN lines follow. Each of these lines contains two integers XX and YY (0X,Y50000≤X,Y≤5000) which describe coordinates of a guard in a 2D plane. Next, QQ lines follow. Each of these lines contains two integers AA and BB (0A,B50000≤A,B≤5000) which describe coordinates of a security incident.

Output

For each of QQ security incidents output a line containing shortest distance to any of the security guards, measured in steps.

Examples

Input
2 3
0 1
4 0
5 0
4 3
1 2
Output
1
3
1
Input
2 4
0 0
3 3
1 1
0 3
1 2
3 3
Output
1
3
2
0

题意:N个消防队,位于二维平面坐标上,Q个地方发生了火灾,求离此火灾位置最近的消防队的距离

最近距离的定义建立在8个方向上。

分析:需要预处理一下,不然会超时,接下来就是普通的bfs.

AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn = 5010;
 8 #define LL long long
 9 #define INF 0x3f3f3f3f
10 int N,Q;
11 int dis[maxn][maxn];
12 typedef struct node{
13     int x,y,w;
14 }node;
15 queue<node> que;
16 int dx[]={0,1,1,1,0,-1,-1,-1};
17 int dy[]={1,1,0,-1,-1,-1,0,1};
18 void bfs(){
19     while(!que.empty()){
20         node p=que.front();
21         que.pop();
22         for(int i=0;i<8;i++){
23             int xx=p.x+dx[i];
24             int yy=p.y+dy[i];
25             if(xx<0||xx>5000||yy<0||yy>5000) continue;
26             if(dis[xx][yy]==-1){
27                 dis[xx][yy]=p.w+1;
28                 que.push(node{xx,yy,p.w+1});
29             }
30         }
31     }
32 }
33 int main(){
34     scanf("%d%d",&N,&Q);
35     memset(dis,-1,sizeof(dis));
36     while(!que.empty()) que.pop();
37     for(int i=0;i<N;i++){
38         node tmp;
39         scanf("%d%d",&tmp.x,&tmp.y);
40         tmp.w=0;
41         que.push(tmp);
42 //        cout<<"!!!!!!!!"<<endl;
43         dis[tmp.x][tmp.y]=0;
44     }
45     bfs();
46     for(int i=0;i<Q;i++){
47         int a,b;
48         scanf("%d%d",&a,&b);
49         printf("%d\n",dis[a][b]);
50     }
51 
52     return 0;
53 }
54 /*
55 2 3
56 0 1
57 4 0
58 5 0
59 4 3
60 1 2
61 */

 

posted @ 2019-08-15 19:25  Brave_WTZ  阅读(405)  评论(0编辑  收藏  举报