首页 > 代码库 > HDU-4643-GSM(DFS)
HDU-4643-GSM(DFS)
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.
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
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.
思路:每次获取中点的近期的基站的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)