ZOJ-3900-Three Circles

ZOJ-3900-Three Circles

                        Time Limit: 2 Seconds      Memory Limit: 65536 KB

There are three circles in the plane. And some of them may coincide. Your task is to find a circle orthogonal to all the three circles.

Two circle are orthogonal to each other if one of them cuts the other at right angles.

Input

There are multiple test cases. The first line of input contains an integer T(T≤ 10000) indicating the number of test cases. For each test case:

There are 9 integers one line: x1, y1, r1, x2, y2, r2 and x3, y3, r3. (xi, yi) and ri are the center and the radius of the i-th circle. (1≤ xi, yi, ri≤ 105)

Output

Each test case in one line: output the coordinate the center of the circle you found if possible, otherwise, output “Impossible!”. The coordinates should be integers or irreducible fractions. If there are multiple solutions, you can choose arbitrary one. And each line you output should be no more than 100 characters.

Sample Input

3
5 5 1 5 5 2 5 5 3
1 3 1 3 1 1 4 4 1
1 3 1 3 1 1 5 3 1
Sample Output

Impossible!
11/4 11/4
3 3

Hint

The second sample and the third sample:

ZOJ-3900-Three Circles_第1张图片

题目链接:ZOJ 3900

题目思路:转,大神的代码。

以下是代码:

import java.io.*;
import java.math.BigInteger;

public class Main {
    static int[] x = new int[3], y = new int[3], r = new int[3];
    static PrintWriter out;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(new OutputStreamWriter(System.out));

        for (int T = Integer.parseInt(br.readLine()); T > 0; --T) {
            String[] tok = br.readLine().split(" ");
            for (int i = 0; i < 3; ++i) {
                x[i] = Integer.parseInt(tok[i * 3]);
                y[i] = Integer.parseInt(tok[i * 3 + 1]);
                r[i] = Integer.parseInt(tok[i * 3 + 2]);
            }
            sol();
        }
        br.close();
        out.close();
    }

    static long sqd(int x, int y) {
        return (long) (x - y) * (long) (x + y);
    }

    static void sol() {
        long[] a = new long[3], b = new long[3], c = new long[3];
        for (int j = 0; j < 2; ++j) {
            a[j] = (x[j] - x[2]) * 2;
            b[j] = (y[j] - y[2]) * 2;
            c[j] = sqd(r[2], r[j]) + sqd(x[j], x[2]) + sqd(y[j], y[2]);
        }
        for (int i = 0; i < 3; ++i) {
            int j = (i + 1) % 3;
            if (a[i] == a[j] && b[i] == b[j] && c[i] != c[j]) {
                out.println("Impossible!");
                return;
            }
        }
        long d = a[0] * b[1] - a[1] * b[0], dx = c[0] * b[1] - c[1] * b[0], dy = a[0] * c[1] - a[1] * c[0];
        if (d == 0) {
            if (dx != 0 || dy != 0) {
                out.println("Impossible!");
            } else {
                int k = 0;
                for (; k < 2 && a[k] == 0L && b[k] == 0L; ++k)
                    ;
                if (k == 2) {
                    print(-100000, 1);
                    out.append(' ');
                    print(-100000, 1);
                    out.append('\n');
                } else {
                    if (b[k] == 0L) {
                        print(c[k], a[k]);
                        out.append(' ');
                        print(-100000, 1);
                        out.append('\n');
                    } else {
                        print(-100000, 1);
                        out.append(' ');
                        print(c[k] + 100000 * a[k], b[k]);
                        out.append('\n');
                    }
                }
            }
            return;
        }
        for (int i = 0; i < 3; ++i)
            if (!outer(x[i], y[i], r[i], dx, dy, d)) {
                out.println("Impossible!");
                return;
            }
        print(dx, d);
        out.append(' ');
        print(dy, d);
        out.append('\n');
    }

    static void print(long n, long d) {
        long g = gcd(n, d);
        n /= g;
        d /= g;
        if (d < 0) {
            n = -n;
            d = -d;
        }
        out.print(n);
        if (d != 1)
            out.print("/" + d);
    }

    static long gcd(long a, long b) {
        return b == 0 ? a : gcd(b, a % b);
    }

    static boolean outer(long x, long y, long r, long dx, long dy, long d) {
        BigInteger X = BigInteger.valueOf(x), Y = BigInteger.valueOf(y), R = BigInteger.valueOf(r),
                D = BigInteger.valueOf(d);
        X = X.multiply(D).subtract(BigInteger.valueOf(dx)).pow(2);
        Y = Y.multiply(D).subtract(BigInteger.valueOf(dy)).pow(2);
        R = R.multiply(D).pow(2);
        return X.add(Y).compareTo(R) > 0;
    }
}

你可能感兴趣的:(ZOJ,3900)