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. 利用jquery制作滚动到指定位置触发动画

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>利用 ...

  2. JSP的九大对象和四大作用域

    1.JSP中九大内置对象为: request            请求对象          类型 javax.servlet.ServletRequest        作用域   Request ...

  3. flutter 上传图片 image_picker 的使用

    Github地址: https://github.com/flutter/plugins/tree/master/packages/image_picker packages地址: https://p ...

  4. CSU 2018年12月月赛 A 2213: Physics Exam

    Description 高中物理老师总认为给学生文本形式的问题比给纯计算形式的问题要求更高.毕竟,学生首先得阅读和理解问题. 因此,他们描述一个问题不像”U=10V,I=5A,P=?”,而是”有一个含 ...

  5. ELK6.3.2+filebeat部署过程

    ELK安装部署 elk作为公司的日志收集检索的方案的首选,是必要的工具,下面介绍一下elk的安装部署方法,以及一些报错的解决方法:(使用的是ubuntu16.04,jdk使用1.8,ELK的版本为6. ...

  6. 升级openssh踩得坑

    升级背景: 项目中使用的系统为CentOS6.8,经过漏洞扫描后发现openssh高危漏洞,具体描述如下:OpenSSH 7.2p2之前版本, sshd/ session.c/ do_setup_en ...

  7. Quartz--01

    Quartz 调度器(scheduler):定时定频率的去执行任务 任务(job):业务逻辑 触发器(trigger):让任务生效的时间 JobDetail(包含任务实现类,任务信息) trigger ...

  8. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  9. UVA 213 信息解码(二进制&位运算)

    题意: 出自刘汝佳算法竞赛入门经典第四章. 考虑下面的01串序列: 0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, …, 1 ...

  10. 《ajax学习》之ajax+JavaScript事件验证用户名是否可注册

    当用户注册时,服务器数据库需要对用户输入的用户信息(以用户名为例子)进行验证,在不刷新页面的情况下又需要页面和服务器进行数据请求,最好的方法是用ajax异步请求. 一.实现思路: 1.用户输入信息 2 ...