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

资源下载链接为: https://pan.quark.cn/s/6b3e936ec683 在英语学习过程中,一款优秀的词典工具至关重要。Vocabulary.com Dictionary 和欧陆词典(EuroDict)作为两款备受推崇的在线词汇资源,各具特色且能够相互补充,为用户打造全面的词汇学习体验。 Vocabulary.com Dictionary 不仅提供单词的标准释义,还特别注重词汇的实际运用。它涵盖了丰富的例句、短语和习语,帮助用户掌握词汇在不同语境中的使用方式。此外,Vocabulary.com 设有互动学习功能,通过游戏和挑战的形式,让学习者在趣味中巩固新词汇。其“智能学习计划”能够根据用户的学习进度和能力定制个性化学习路径,是提升词汇量的有效工具。 与之配合的欧陆词典则以多语言支持和深度词汇解析闻名。它不仅提供英文词汇的解释,还涵盖多种语言对照,非常适合多语种学习者。欧陆词典还提供同义词、反义词、派生词等扩展信息,以及丰富的短语和习语,帮助用户全面理解词汇的多维度含义。 在实际使用时,学习者可以先通过 Vocabulary.com Dictionary 查找单词的基本信息和应用场景,再借助欧陆词典拓展对词汇的多语言理解,尤其是对比不同语言中词汇的对应关系。Vocabulary.com 的互动学习模式适合日常学习,而欧陆词典则更适合深度研究和词汇拓展。 压缩包中的文件可能包括“Vocabulary.com Dictionary.jpg”,这可能是词典的截图或封面,用于视觉介绍;“Vocabulary.com Dictionary.mdd”和“.mdx”文件则是欧陆词典的数据文件,用于存储索引和数据,方便离线查询。将这些文件下载到本地,即使在无网络的情况下,也能使用部分功能。 Vocabulary.com Dictionary 和欧陆词典的结合使用,能为学习者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值