HDU-4643-GSM(DFS)

本文深入探讨了移动开发领域的核心技术,包括iOS开发、Android开发、鸿蒙开发等,提供了丰富的实践经验和前沿技术趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


思路:每次获取中点的最近的基站的id,id跟哪边不一样就往哪边递归查找。


#include <stdio.h>
#define INF 99999999

int n,m,ans;
bool vis[50];
double sx[50],sy[50],ex[50],ey[50];

int get(double x,double y)//获取离该点最近的基站
{
    double mn=INF;
    int id,i;

    for(i=0;i<m;i++)
    {
        if((x-ex[i])*(x-ex[i])+(y-ey[i])*(y-ey[i])<mn)
        {
            mn=(x-ex[i])*(x-ex[i])+(y-ey[i])*(y-ey[i]);
            id=i;
        }
    }

    return id;
}

void dfs(int l,int r,double lx,double ly,double rx,double ry)//递归查找,每次获取中点的最近的基站的id,id跟哪边不一样就往哪边找。
{
    if((lx-rx)*(lx-rx)+(ly-ry)*(ly-ry)<1e-14) return;

    double x=(lx+rx)/2.0;
    double y=(ly+ry)/2.0;

    int id=get(x,y);

    if(!vis[id])
    {
        vis[id]=1;
        ans++;
    }

    if(id!=l) dfs(l,id,lx,ly,x,y);

    if(id!=r) dfs(id,r,x,y,rx,ry);
}

int main()
{
    int i,q,id1,id2;
    int from,to;

    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++) scanf("%lf%lf",&sx[i],&sy[i]);

        for(i=0;i<m;i++) scanf("%lf%lf",&ex[i],&ey[i]);

        scanf("%d",&q);

        while(q--)
        {
            scanf("%d%d",&from,&to);

            id1=get(sx[from-1],sy[from-1]);
            id2=get(sx[to-1],sy[to-1]);

            if(id1==id2) printf("0\n");
            else
            {
                for(i=0;i<m;i++) vis[i]=0;
                vis[id1]=1;
                vis[id2]=1;
                ans=1;
                dfs(id1,id2,sx[from-1],sy[from-1],sx[to-1],sy[to-1]);
                printf("%d\n",ans);
            }
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值