Cod sursa(job #1486893)

Utilizator andreiiiiPopa Andrei andreiiii Data 15 septembrie 2015 18:04:30
Problema Robot Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 8.38 kb
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream;
        try {
            inputStream = new FileInputStream("robot.in");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream("robot.out");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Robot solver = new Robot();
        solver.solve(1, in, out);
        out.close();
    }

    static class Robot {
        private final double eps = 1e-10;

        private double distSquare(Point a, Point b) {
            return (a.x - b.x) * (a.x - b.x) +
                    (a.y - b.y) * (a.y - b.y);
        }

        private double slope(Point a, Point b) {
            if (a.x == b.x) return 1e99;
            return (a.y - b.y) / (a.x - b.x);
        }

        private double crossProduct(Point o, Point a, Point b) {
            return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            Point[] robot = new Point[N];
            for (int i = 0; i < N; ++i) {
                robot[i] = new Point(in.nextDouble(), in.nextDouble());
            }

            int M = in.nextInt();
            Point[][] obstacles = new Point[M][];
            for (int i = 0; i < M; ++i) {
                int K = in.nextInt();
                obstacles[i] = new Point[K];
                for (int j = 0; j < K; ++j)
                    obstacles[i][j] = new Point(in.nextDouble(), in.nextDouble());
            }

            for (int i = 0; i < M; ++i)
                obstacles[i] = expand(obstacles[i], robot);

            double minx = robot[0].x, miny = robot[0].y;
            for (int i = 1; i < N; ++i) {
                minx = Math.min(minx, robot[i].x);
                miny = Math.min(miny, robot[i].y);
            }
            out.println(-1);
            return;

            //System.err.println(Arrays.toString(obstacles[0]));

        /*Point start = new Point(minx, miny), end = new Point(in.nextDouble(), in.nextDouble());
        if (!checkPoint(end, obstacles)) {
            out.println(-1);
            return;
        }

        int countPoints = 2;
        for (int i = 0; i < M; ++i)
            countPoints += obstacles[i].length;

        Point[] allPoints = new Point[countPoints];
        allPoints[0] = start;
        countPoints = 1;
        for (Point[] obstacle: obstacles)
            for (Point p: obstacle)
                allPoints[countPoints++] = p;

        allPoints[countPoints++] = end;

        //System.err.flush();

        double ans = shortestPath(allPoints, obstacles, 0, countPoints - 1);
        out.println(String.format("%.2f", ans));*/
        }

        private Point[] expand(Point[] poly1, Point[] poly2) {
            int N = poly1.length, M = poly2.length;
            double minx = poly2[0].x, miny = poly2[0].y;
            for (int i = 1; i < M; ++i) {
                minx = Math.min(minx, poly2[i].x);
                miny = Math.min(miny, poly2[i].y);
            }

            Point[] allPoints = new Point[N * M];
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < M; ++j) {
                    double nx = poly1[i].x - poly2[j].x + minx;
                    double ny = poly1[i].y - poly2[j].y + miny;
                    allPoints[M * i + j] = new Point(nx, ny);
                }
            }
            //System.err.println(Arrays.toString(allPoints));
            //System.err.flush();

            return convexHull(allPoints);
        }

        private Point[] convexHull(Point[] points) {
            int pos = 0;
            for (int i = 1; i < points.length; ++i)
                if (points[i].x < points[pos].x ||
                        (Math.abs(points[i].x - points[pos].x) < eps &&
                                points[i].x < points[pos].y)) pos = i;

            if (pos != 0) {
                Point aux = points[pos];
                points[pos] = points[0];
                points[0] = aux;
            }

            final Point ref = points[0];
            Arrays.sort(points, 1, points.length, new Comparator<Point>() {
                public int compare(Point o1, Point o2) {
                    double angle1 = slope(ref, o1), angle2 = slope(ref, o2);
                    if (Math.abs(angle1 - angle2) < eps)
                        return Double.compare(distSquare(ref, o1), distSquare(ref, o2));
                    return Double.compare(angle1, angle2);
                }
            });

            Point[] result = new Point[points.length];
            int top = 0;
            for (Point point : points) {
                while (top >= 2 && crossProduct(result[top - 2], result[top - 1], point) < eps) top--;
                result[top++] = point;
            }
            return Arrays.copyOf(result, top);
        }

        private class Point {
            double x;
            double y;

            Point(double x, double y) {
                this.x = x;
                this.y = y;
            }

            public boolean equals(Object e) {
                if (!(e instanceof Point)) return false;
                Point other = (Point) e;
                return Math.abs(x - other.x) < eps && Math.abs(y - other.y) < eps;
            }

            public String toString() {
                return String.format("{%.2f, %.2f}", x, y);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new UnknownError();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}