zoukankan      html  css  js  c++  java
  • HDU-4643-GSM(DFS)

    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
    ");
                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
    ",ans);
                }
            }
        }
    }
    


  • 相关阅读:
    Entity Framework 同一个上下文中,如何进行对同一个实体进行指定字段更新
    【PS必备】各系色卡对照表
    【简易教程】人体时钟 ホネホネ・クロック [時計・ブログパーツ]
    设计产品网站
    未将对象引用设置到对象的实例--可能出现的问题总结
    转 zabbix 自动发现和 zabbix自定义用户key与参数User parameters
    转 RAC单个节点执行expdp提示ORA-31693 ORA-31617 ORA-19505 ORA-27037错误
    转 : 请问mysql如何确定一个库是主库还是从库。
    转 zabbix debug and zabbix使用percona插件监控mysql
    转 Linux sudo命令
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7122495.html
Copyright © 2011-2022 走看看