2012-06-05 07:37:04Morris

[ACM-ICPC] 5861 - Hidden Terminal Problem

Ad hoc networks are wireless networks with no fixed infrastructure. Each device in the network functions as a router that discovers and maintains routes for other nodes. Suppose that the communication range (radius) of each device is R unit length. Device A can communicate with device B directly, when the distance between A and B is less than or equal to R. Specifically, device A can communicate with device B directly if (x1 - x2)2 + (y1 - y2)2 $ leq$ R2, where (x1, y1) and (x2, y2) are, respectively, the locations of A and B.

The hidden terminal problem in ad hoc networks is caused by concurrent transmissions of two devices that cannot communicate with each other directly, but transmit to the same destination. For example, device A cannot communicate with device B directly (i.e. the distance between A and B is greater than R), but device A can communicate with device C directly (i.e. the distance between A and C is less than or equal to R) and device B can communicate with device C directly. Such three devices are referred to here a hidden-terminal set. For example, there are four devices ABCD, deployed in the plane at (0, 0), (0, 1), (1, 0), (1, 1), respectively. Given that the radius of each device is R = 1 unit length, A can communicate with B (C) directly, and similarly D can communicate with B (C) directly. However, A cannot communicate with D directly and B cannot communicate with C directly. The number of different hidden-terminal sets in the plane is four (i.e., {A, B, C}, {A, B, D}, {A, C, D}, and {B, C, D}).

Hidden-terminal sets in an ad hoc network seriously results in garbled messages and increases communication delay, thus degrading system performance. Given N devices deployed in a plane, please compute the number of different hidden-terminal sets in the network.

Input 

There are at most 10 test cases. The first line of each instance consists of an integer N(3 $ leq$ N $ leq$ 100), where N is the number of devices in the network. The second line of each instance consists of an integer R(1 $ leq$ R $ leq$ 100), where R is the communication range (radius) of each device. Each of the following N lines consists of two integers x and y (separated by a space) (- 99 $ leq$ x $ leq$ 99, -99 $ leq$ y $ leq$ 99), which indicate the location (x-coordinate and y-coordinate) of a device in the plane. The last test case will be followed by a line containing N = 0.

Output 

The output for each instance should contain an integer denoting the number of different hidden-terminal sets in the network.

Sample Input  

4
1
0 0
0 1
1 0
1 1
5
2
1 1
2 2
3 3
4 4
5 5
0

Sample Output 

4
3

窮舉 A(X)B, C-A, C-B
 

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int N, R;
    int i, j, k, x[101], y[101];
    while(scanf("%d", &N) == 1 && N) {
        scanf("%d", &R);
        for(i = 0; i < N; i++)
            scanf("%d %d", &x[i], &y[i]);
        int Ans = 0;
        for(i = 0; i < N; i++) {
            for(j = i+1; j < N; j++) {
                if((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) > R*R)
                    for(k = 0; k < N; k++) {
                        if(k == i || k == j)    continue;
                        if((x[i]-x[k])*(x[i]-x[k]) + (y[i]-y[k])*(y[i]-y[k]) <= R*R &&
                                (x[k]-x[j])*(x[k]-x[j]) + (y[k]-y[j])*(y[k]-y[j]) <= R*R)
                            Ans++;
                    }
            }
        }
        printf("%d\n", Ans);
    }
    return 0;
}