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

HDU-4643-GSM(DFS)的更多相关文章

  1. hdu 4643 GSM 计算几何 - 点线关系

    /* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...

  2. HDU 4643 GSM 算术几何

    当火车处在换基站的临界点时,它到某两基站的距离相等.因此换基站的位置一定在某两个基站的中垂线上, 我们预处理出任意两基站之间的中垂线,对于每次询问,求询问线段与所有中垂线的交点. 检验这些交点是否满足 ...

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

    GSM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...

  4. hdu 4643 GSM(暴力)

    GSM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  5. HDU 4643 GSM 简单计算几何

    今天比赛的时候略坑, admin告诉我询问Q的个数不超过n^2, 赛后敲了个 O(Q*m^3)的复杂度,但这个复杂度常数比较低,可能在除以个小常数, 300ms过了,真心无语,数据应该水了吧,比赛的时 ...

  6. HDU 4643 GSM 暑期多校联合训练第五场 1001

    点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...

  7. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

  10. hdu 1175 连连看 DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...

随机推荐

  1. eclipse中同步代码PULL报错checkout conflict with files的解决方法

    1.Team--->Synchronize Workspace 2.在同步窗口找到冲突文件,把自己本地修改的复制出来 3.在文件上右键选择 Overwrite----->Yes , 4.再 ...

  2. 第2节 mapreduce深入学习:11、maptask运行机制(多看几遍)

    mapTask运行机制详解以及mapTask的并行度在mapTask当中,一个文件的切片大小使用默认值是128M,就是跟我们一个block块对应大小一样 MapTask运行的整个过程 背下来1.Tex ...

  3. Android图像处理之BitMap(2)

    Bitmap 相关 1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: * png图片 如:R.drawable.tianjin J ...

  4. Python学习-列表的修改,删除操作

    列表的修改操作 列表中的许多操作和字符串中有许多的相同点,因为列表是一个有顺序可变的元素集合,所以在列表中可以进行增加,删除,修改,查找的操作. 列表的修改操作: 如果你想单个修改列表中的某一个元素, ...

  5. thinkphp3.2使用PHPQrcode实现二维码

    Thinkphp中没有二维码相关的生成库,百度有不少工具和库 这里就实例一下通过think3.2搭配phpqrcode来完成生成二维码的功能. 至于phpQrcode库文件 百度很容易找到这里也给大家 ...

  6. Android四大核心组件之Activity

    一.活动生命周期 二.生命周期执行介绍 当该页面(Activity)被启动时 会执行onCreate().onStart().onRestart()这三个方法, 只有当onRestart() 方法执行 ...

  7. 建仓类型与对应建仓价MT4

    建仓类型与对应建仓价 (Bid,Ask) 建仓类型 对应建仓价 Buy Ask+Spread Sell Bid-Spread BuyLimit Ask-Spread-StopLevel SellLim ...

  8. <git>……git的基本使用……//

    1.切换到存放git版本库的地方 2.Git clone url(github上的地址) 3.设置全局用户(输入一次即可) git config --global user.name github上的 ...

  9. web前端开发——HTML

    一.简介 1.发展史 (1)web1.0 时代 产物:网页制作 那时的网页主要是静态网页,即没有与用户交互,仅仅是提供信息浏览的网页.如QQ日志.博文等. 网页制作三剑客:Dreamweaver+Fi ...

  10. Leetcode 149.直线上最多的点数

    直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o ...