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

原创 2006年06月15日 15:57:00

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

接收圆和矩形,计算面积,周长

  • 2014年05月21日 01:57
  • 1KB
  • 下载

【OpenCV笔记 15-3】OpenCV计算物体最小包围矩形面积和周长

本文将介绍OpenCV计算物体最小包围矩形面积和周长 两个函数:contourArea()和arcLength 知识点: 1.寻找最小包围矩形 2.计算最小轮廓的面积和长度 3.定义和输出v...

矩形面积计算

  • 2008年01月12日 04:05
  • 108KB
  • 下载

javascript挑战编程技能-第三题:计算矩形房间面积

一、问题:计算矩形房间的面积。 二、要求: 1、提供用户输入长和宽 2、输出英尺和米的单位选择 3、输出平方英尺和平方米的数值 4、结果保留两位有效小数 5、只能输入数值 6、让计算与输出分离 7、使...

QT计算矩形面积

  • 2013年10月23日 19:08
  • 276B
  • 下载

矩形的面积计算

  • 2015年06月18日 15:18
  • 631B
  • 下载

<模板><计算几何>凸包-旋转卡壳最小面积外接矩形

今天做UVA 10173时遇到的问题,网上各位大牛的代码和思想实在无法理解,请教学长后理解了一个算法。 学长代码:http://www.cnblogs.com/jianglangcaijin/arc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TopCoder--计算矩形的公有面积
举报原因:
原因补充:

(最多只允许输入30个字)