TopCoder--计算矩形的公有面积

topcoder.BoxUnion

 

      Problem Statement
      NOTE: This problem statement contains an image that may not display properly if viewed outside of the applet. 
      Given a list of two-dimensional rectangles, compute the area of their union. For example, the union of the three rectangles shown in the figure below:
      cover an area of 35 units.
      The list of rectangles will be given as a String[], where each element describes one rectangle. Each String will be formatted as 4 space-separated integers with no leading zeros, giving the coordinates of the left, bottom, right, and top of the rectangle (in that order). The three rectangles shown above would be given as:
      {{1 3 5 6},
      {3 1 7 5},
      {4 4 9 7}}
      Definition
      Class:
           BoxUnion
      Method:
           area
      Parameters:
           String[]
      Returns:
           int
      Method signature:
           int area(String[] rectangles)
           (be sure your method is public)
     
      Constraints
      -   rectangles will contain between 1 and 3 elements, inclusive.
      -   Each element of rectangles will be formatted as described in the problem statement.
      -   For each rectangle, the left coordinate will be less than the right coordinate and the bottom coordinate will be less than the top coordinate.
      -   All coordinates will be between 0 and 20000, inclusive.
      Examples
      0)
      { "200 300 203 304" }
      Returns: 12
      A single rectangle with area 12.
      1)
      { "0 0 10 10",
      "20 20 30 30" }
      Returns: 200
      Two disjoint rectangles, each of area 100.
      2)
      { "0 500 20000 501",
      "500 0 501 20000" }
      Returns: 39999
      These two rectangles intersect at a single point.
      3)
      { "4 6 18 24",
      "7 2 12 19",
      "0 0 100 100" }
      Returns: 10000
      The third rectangle completely overlaps the first two.
      4)
      { "1 3 5 6",
      "3 1 7 5",
      "4 4 9 7" }
      Returns: 35
      This is the example from the problem statement.
      5)
      { "0 0 20000 20000",
      "0 0 20000 20000",
      "0 0 20000 20000" }
      Returns: 400000000

public class BoxUnion
{
    int[][] intArray = new int[7][5];
    int area = 0;

    public int area(String[] rectangles)
    {
        fillArray(rectangles);
        if (rectangles.length == 1)
        {
            this.area = intArray[0][4];
        }
        else
            if (rectangles.length == 2)
            {
                // do two rectangles
                intArray[2] = fillAChar(intArray[0], intArray[1]);
                this.area = intArray[0][4] + intArray[1][4] - intArray[2][4];
            }
            else
                if (rectangles.length == 3)
                {
                    // do three rectangles
                    intArray[3] = fillAChar(intArray[0], intArray[1]);
                    intArray[4] = fillAChar(intArray[0], intArray[2]);
                    intArray[5] = fillAChar(intArray[1], intArray[2]);
                    intArray[6] = fillAChar(intArray[3], intArray[4]);
                    this.area = intArray[0][4] + intArray[1][4] + intArray[2][4] - intArray[3][4] - intArray[4][4]
                            - intArray[5][4] + intArray[6][4];
                }
        return this.area;
    }

    int[] fillAChar(int[] a, int[] b)
    {
        int[] c = new int[5];
        c[0] = (a[0] > b[0]) ? a[0] : b[0];
        c[1] = (a[1] > b[1]) ? a[1] : b[1];
        c[2] = (a[2] < b[2]) ? a[2] : b[2];
        c[3] = (a[3] < b[3]) ? a[3] : b[3];
        if ((c[3] < c[1]) || c[2] < c[0])
        {
            c[4] = 0;
        }
        else
        {
            c[4] = (c[3] - c[1]) * (c[2] - c[0]);
        }
        return c;
    }

    void fillArray(String[] str)
    {
        int i;
        for (i = str.length - 1; i >= 0; i--)
        {
            // System.out.println(str[i] + "/n");
            int j = 0;
            int k = 0;
            int counter = 0;
            while (counter < 3)
            {
                k = j;
                j = str[i].indexOf(" ", k);
                Integer it = new Integer(str[i].substring(k, j));
                this.intArray[i][counter] = it.intValue();
                counter++;
                j++;
            }
            Integer it = new Integer(str[i].substring(j));
            this.intArray[i][counter] = it.intValue();

            // 判断输入的数是否非法,非法的话打印出来。
            if (intArray[i][2] < intArray[i][0] || intArray[i][3] < intArray[i][0])
            {
                System.out.println("Input numbers ERROR:");
            }
            else
                if (intArray[i][0] < 0 || intArray[i][1] < 0 || intArray[i][2] < 0 || intArray[i][3] < 0)
                {
                    System.out.println("Input numbers ERROR!");
                }
            // 计算每个矩形的面积
            intArray[i][4] = (intArray[i][2] - intArray[i][0]) * (intArray[i][3] - intArray[i][1]);
            // System.out.println(intArray[i][4]);
        }
    }

    public static void main(String[] args)
    {
        BoxUnion bu = new BoxUnion();

        String[] str = new String[]
        /*
         * { "0 0 20000 20000", "0 0 20000 20000", "0 0 20000 20000" }; { "200 300 203 304" }; { "1 3 5 6", "3 1 7 5",
         * "4 4 9 7" } { "0 0 10 10", "20 20 30 30" } { "0 0 20000 20000", "0 0 20000 20000", "0 0 20000 20000" } { "4 6
         * 18 24", "7 2 12 19", "0 0 100 100" }
         */
        { "0 500 20000 501", "500 0 501 20000" };

        System.out.println("The area is: " + bu.area(str));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值