Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1906133
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2006-08-14 15:41:31

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));
    }
}

阅读(1268) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~