HDU 4643 GSM (2013多校5 1001题 计算几何)

GSM

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 622    Accepted Submission(s): 206

Problem Description Xiao Ming is traveling around several cities by train. And the time on the train is very boring, so Xiao Ming will use the mobile Internet. We all know that mobile phone receives the signal from base station and it will change the base station when moving on the train. Xiao Ming would like to know how many times the base station will change from city A to city B.

Now, the problem is simplified. We assume the route of train is straight, and the mobile phone will receive the signal from the nearest base station.   

 

Input Multiple cases. For each case, The first line: N(3<=N<=50) – the number of cities, M(2<=M<=50) – the number of base stations. Then there are N cities with coordinates of (x, y) and M base stations with coordinates of (x, y) – (0<=x<=1000, 0<=y<=1000, both x and y is integer).Then there is a number : K, the next, there are K queries, for each query, each line, there are two numbers: a, b.  

 

Output For each query, tell Xiao Ming how many times the base station will change from city a to city b.  

 

Sample Input 4 4 0 2 1 3 1 0 2 0 1 2 1 1 2 2 2 1 4 1 2 1 3 1 4 3 4  

 

Sample Output 0 1 2 1
Hint The train way from a to b will not cross the point with the same distance from more than 2 base stations. (For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2). And every city exactly receive signal from just one base station.  

 

Source
2013 Multi-University Training Contest 5  

 

Recommend zhuyuanchen520  

 

 

在从u->v的路径上,不断分成两段去做。

很简单

 

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
using namespace std;

const double eps = 1e-8;
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
};
//*两点间距离
inline double dis(Point a,Point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
Point p1[55],p2[55];
int n,m;
inline int Belong(Point p)
{
    int k = 0;
    double d = dis(p,p2[0]);
    for(int i = 1;i < m;i++)
    {
        double d2 = dis(p,p2[i]);
        if(d2 < d)
        {
            d = d2;
            k = i;
        }
    }
    return k;
}
int solve(Point a,Point b)
{
    int k1 = Belong(a);
    int k2 = Belong(b);
    if(k1 == k2)return 0;
    if(dis(a,b)<eps)return 1;
    Point t = Point((a.x+b.x)/2,(a.y+b.y)/2);
    return solve(a,t)+solve(t,b);
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&m) == 2)
    {
        for(int i = 0;i < n;i++)
            p1[i].input();
        for(int i = 0;i < m;i++)
            p2[i].input();
        int K;
        int u,v;
        scanf("%d",&K);
        while(K--)
        {
            scanf("%d%d",&u,&v);
            u--;v--;
            printf("%d\n",solve(p1[u],p1[v]));
        }
    }
    return 0;
}

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3244132.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞