HDU 4643 GSM(计算几何)

题目链接

题目描述:
GSM

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

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.

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

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=50+5;
const double eps=1e-8;
const double inf=0x3f3f3f3f;
int n,m;
struct point
{
    double x,y;
    point(double a=0,double b=0): x(a),y(b) {}

};
double len(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

point p1[maxn],p2[maxn];

int find(point p)
{
    double _min=len(p,p2[1]),k=1;
    for(int i=2;i<=m;i++)
    {
        double dis=len(p,p2[i]);
        if(dis<_min)
        {
            k=i;
            _min=dis;
        }
    }
    return k;
}

int solve(point a,point b)
{
    int p=find(a);
    int q=find(b);
    if(p==q) return 0;
    else if(len(a,b)<eps) return 1;
    else 
    {
        point mid=point((a.x+b.x)/2,(a.y+b.y)/2);
        return solve(a,mid)+solve(mid,b);
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(p1,0,sizeof(p1));
        memset(p2,0,sizeof(p2));
        for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p1[i].x,&p1[i].y);
        for(int i=1;i<=m;i++)
        scanf("%lf%lf",&p2[i].x,&p2[i].y);
        int k;
        scanf("%d",&k);
        while(k--)
        {
            int p,q;
            scanf("%d%d",&p,&q);
            printf("%d\n",solve(p1[p],p1[q]));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值