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); } } } }



  • 相关阅读:
    面试题: 数据库 已看1 group by 和order by的练习 sql语句练习简单 有用
    笔试题: 数据库 已看1 一些关键的sql语句练习 和选择题 有用 sql语句练习 挺好
    面试题: mysql数据库 已看1 简单的sql练习
    面试题: mysql 数据库已看 sql安全性 索引 引擎 sql优化
    面试题: redis面试题 有用 redis详细
    面试题: mysql 数据库去重 已看1 不好使
    面试题: 数据库已看1 视图存储过程 没用
    面试题: mysql数据库 已看1 索引和事务 没用
    面试题: 数据库 oracle数据库 已看1 意义不大 有用
    LeetCode--Valid Palindrome
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7122494.html
Copyright © 2011-2022 走看看