首页 > 专题系列 > Java解POJ > POJ 3335 Rotating Scoreboard [解题报告] Java
2013
11-12

POJ 3335 Rotating Scoreboard [解题报告] Java

Rotating Scoreboard

问题描述 :

This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the edges of the polygon. We want to place a rotating scoreboard somewhere in the hall such that a spectator sitting anywhere on the boundary of the hall can view the scoreboard (i.e., his line of sight is not blocked by a wall). Note that if the line of sight of a spectator is tangent to the polygon boundary (either in a vertex or in an edge), he can still view the scoreboard. You may view spectator’s seats as points along the boundary of the simple polygon, and consider the scoreboard as a point as well. Your program is given the corners of the hall (the vertices of the polygon), and must check if there is a location for the scoreboard (a point inside the polygon) such that the scoreboard can be viewed from any point on the edges of the polygon.

输入:

The first number in the input line, T is the number of test cases. Each test case is specified on a single line of input in the form n x1 y1 x2 y2xn yn where n (3 ≤ n ≤ 100) is the number of vertices in the polygon, and the pair of integers xi yi sequence specify the vertices of the polygon sorted in order.

输出:

The output contains T lines, each corresponding to an input test case in that order. The output line contains either YES or NO depending on whether the scoreboard can be placed inside the hall conforming to the problem conditions.

样例输入:

2
4 0 0 0 1 1 1 1 0
8 0 0  0 2  1 2  1 1  2 1  2 2  3 2  3 0

样例输出:

YES
NO

解题代码:

import java.util.Scanner;  
  
public class Main{  
  
    static double x[], y[];  
    static double tx[], ty[];  
    static double txp[], typ[];  
    static int num;  
    static int tnum;  
    static double a, b, c;  
  
    public static void main(String[] args) {  
  
        Scanner scan = new Scanner(System.in);  
        int t = scan.nextInt();  
        for(int test=0;test< t;test++){  
  
            int n = scan.nextInt();  
            num = n;  
  
            x = new double[105];  
            y = new double[105];  
            tx = new double[105];  
            ty = new double[105];  
            txp = new double[105];  
            typ = new double[105];  
  
            for (int i = 0; i < n; i++) {  
                x[i] = scan.nextDouble();  
                y[i] = scan.nextDouble();  
                tx[i + 1] = x[i];  
                ty[i + 1] = y[i];  
            }  
  
            x[n] = x[0];  
            y[n] = y[0];  
            tx[0] = tx[n];  
            ty[0] = ty[n];  
            tx[n + 1] = tx[1];  
            ty[n + 1] = ty[1];  
  
            for (int i = 0; i < n; i++) {  
                a = y[i + 1] - y[i];  // 求aX+bY+c = 0;的a,b,c   
                b = x[i] - x[i + 1];  
                c = x[i + 1] * y[i] - x[i] * y[i + 1];  
                solve();  
            }  
  
            if (num == 0)  
                System.out.println("NO");  
            else  
                System.out.println("YES");  
  
        }  
  
    }  
  
    public static void solve() {  
  
        tnum = 0;  
  
        for (int i = 1; i <= num; i++) {  
  
            if (sig(a * tx[i] + b * ty[i] + c) >= 0) {  //在一侧   
                txp[tnum] = tx[i];  
                typ[tnum++] = ty[i];  
            } else {                                     // 在别一侧   
                if (sig(a * tx[i - 1] + b * ty[i - 1] + c) > 0)   // 大于0才会有交点   
                    insert(tx[i - 1], ty[i - 1], tx[i], ty[i]);  
                if (sig(a * tx[i + 1] + b * ty[i + 1] + c ) > 0)  
                    insert(tx[i + 1], ty[i + 1], tx[i], ty[i]);  
            }  
        }  
  
        num = tnum;              // 更新tx,ty,num   
  
        for (int j = 1; j <= num; j++) {  
            tx[j] = txp[j - 1];  
            ty[j] = typ[j - 1];  
        }  
        tx[0] = tx[num];  
        ty[0] = ty[num];  
        tx[num + 1] = tx[1];  
        ty[num + 1] = ty[1];  
  
    }  
  
    private static int sig(double d) {  
        if(d< -(1e-10))  
            return -1;  
        else if(d>1e-10)  
            return 1;  
        return 0;  
    }  
  
    public static void insert(double x1, double y1, double x2, double y2) {  
  
        double xx = Math.abs(a * x1 + b * y1 + c); //求两直线交点   
        double yy = Math.abs(a * x2 + b * y2 + c);  
        txp[tnum] = (x1 * yy + x2 * xx) / (xx + yy);  
        typ[tnum++] = (y1 * yy + y2 * xx) / (xx + yy);  
  
    }  
  
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。