首页 > 代码库 > HDU-4643-GSM(DFS)
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.
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 1HintThe 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
2013 Multi-University Training Contest 5
思路:每次获取中点的近期的基站的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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。