HDU-4643-GSM (DFS)

Source: Internet
Author: User
Problem descriptionxiao 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 numbers es the signal from base station and it will change the base station when moving on the train. xiao Ming wowould 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.
 
Inputmultiple 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.
Outputfor each query, tell Xiao Ming how many times the base station will change from City A to City B.
Sample Input
4 40 21 31 02 01 21 12 22 141 21 31 43 4
 
Sample output
0121HintThe 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. 
 
Source2013 multi-university training contest 5


Idea: Obtain the ID of the nearest base station in the midpoint each time. If the ID is different from the ID, Recursively search for the edge.


# Include <stdio. h> # define INF 99999999int n, m, ans; bool vis [50]; double SX [50], Sy [50], ex [50], ey [50]; int get (Double X, Double Y) // obtain the base station {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) // recursive search. Each time you obtain the ID of the nearest base station in the midpoint, you can find the edge where the ID is different from the 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,; while (~ Scanf ("% d", & N, & M) {for (I = 0; I <n; I ++) scanf ("% lf ", & SX [I], & Sy [I]); for (I = 0; I <m; I ++) scanf ("% lf ", & Ex [I], & ey [I]); scanf ("% d", & Q); While (Q --) {scanf ("% 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 );}}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.