Security Guards——BFS

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44056753/article/details/99642384

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 1, 0 or −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 N and Q (1≤N,Q≤3⋅105), the number of guards and the number of security incidents, respectively. After that, N lines follow. Each of these lines contains two integers X and Y (0≤X,Y≤5000) which describe coordinates of a guard in a 2D plane. Next, Q lines follow. Each of these lines contains two integers A and B (0≤A,B≤5000) which describe coordinates of a security incident.

Output

For each of Q 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

Note

Bonus: solve this problem with coordinates up to 1018 in absolute value!

题目链接

题意

在一个5000*5000的网络上,有N个保安,然后给了Q个地点,问距离最近的保安的距离是多少。

分析

首先由于N,Q的数量级比较大,所以不能使用暴搜,很容易想到使用BFS,但是需要注意要使用BFS对保安进行预处理(类似于打表),而不是对地点进行寻找保安的BFS,比赛的时候就是对地点使用BFS,结果一直TL。还要注意一点,题目中的距离时按步数来的,而不是直接求距离,保安的八个方向的单位长度都是1。

下面是比赛时写的TL了N遍,搞炸心态的代码,结果正确,但是TL。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=300001;
int dic[8][2]= {{-1,0},{1,0},{0,1},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
int baoan[6000][6000];
int vis[6000][6000];
vector<int> ans;
struct peo
{
    int x,y,step;
};
peo in[maxn];
int bfs(peo p)
{
    int num=0;
    if(baoan[p.x][p.y]==1)
        return 0;
    queue<peo> aa;
    aa.push(p);
    vis[p.x][p.y]=1;
    while(!aa.empty())
    {
        peo wu=aa.front();
        aa.pop();
        wu.step++;
        for(int i=0; i<8; i++)
        {
            int x,y;
            x=wu.x+dic[i][0];
            y=wu.y+dic[i][1];
            if(vis[x][y]==0&&x>=0&&x<=5000&&y>=0&&y<=5000)
            {
                peo ha;
                ha.x=x,ha.y=y,ha.step=wu.step;
                vis[x][y]=1;
                if(baoan[x][y]==1)
                    return wu.step;
                aa.push(ha);
            }
        }
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    for(int i=0; i<m; i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        baoan[x][y]=1;
    }
    for(int i=0; i<n; i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        in[i].x=x;
        in[i].y=y;
        in[i].step=0;
        memset(vis,0,sizeof(vis));
        cout<<bfs(in[i])<<endl;
    }
}

下面是赛后又写的AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=300001;
const int _i=5001;
int dic[8][2]= {{-1,0},{1,0},{0,1},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
int dis[_i][_i];
struct peo
{
    int x,y,step;
    peo(int a=0,int b=0)
    {
        x=a;
        y=b;
        step=0;
    }
};
peo in[maxn];
int vis[_i][_i];
queue<peo> aa;
void bfs()
{
    peo temp,top;
    while(!aa.empty())
    {
        top=aa.front();
        aa.pop();
        dis[top.x][top.y]=top.step;
        for(int i=0; i<8; i++)
        {
            temp.x=top.x+dic[i][0];
            temp.y=top.y+dic[i][1];
            int x=temp.x,y=temp.y;
            if(!vis[x][y]&&x>=0&&x<=5000&&y>=0&&y<=5000)
            {
                temp.step=top.step+1;
                vis[temp.x][temp.y]=1;
                aa.push(temp);
            }
        }
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    for(int i=0; i<m; i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        peo p(x,y);
        vis[x][y]=1;
        aa.push(p);
    }
    bfs();
    for(int i=0; i<n; i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        printf("%d\n",dis[x][y]);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44056753/article/details/99642384
今日推荐