Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1904918
  • 博文数量: 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:54:36

这是这矩形共享面积的第二个解决办法.与算法1相比,效率方面我感觉各有利弊,贴出来共同研究.
public class BoxUnion2
{
    int[][] intArray;
    int N;

    public int area(String[] rectangles)
    {
        N = rectangles.length;
        int area = 0;
        intArray = new int[rectangles.length + 1][5];
        fillArray(rectangles);
        intArray[rectangles.length] = intArray[0];
        // 找出包含所有矩形的最小矩形
        for (int i = rectangles.length; i > 0; i--)
        {
            if (intArray[rectangles.length][0] > intArray[i][0])
            {
                intArray[rectangles.length] = intArray[0];
            }
            if (intArray[rectangles.length][1] > intArray[i][1])
            {
                intArray[rectangles.length][1] = intArray[i][1];
            }
            if (intArray[rectangles.length][2] < intArray[i][2])
            {
                intArray[rectangles.length][2] = intArray[i][2];
            }
            if (intArray[rectangles.length][3] < intArray[i][3])
            {
                intArray[rectangles.length][3] = intArray[i][3];
            }
        }

        for (int i = intArray[rectangles.length][1]; i < intArray[rectangles.length][1]; i++)
        {
            for (int j = intArray[rectangles.length][1]; j < intArray[rectangles.length][1]; j++)
            {
                if (judgeN(i, j))
                {
                    area++;
                }
            }
        }
        return area;
    }

    // 判断一个方块是否在
    boolean judgeN(int row, int col)
    {
        for (int i = this.N; i >= 0; i--)
        {
            if (judge1(row, col, i)) { return true; }
        }
        return false;
    }

    // 判断一个方块是否在一个矩形内,是的话返回true,不是的话返回false
    boolean judge1(int row1, int col1, int n)
    {
        if (row1 < intArray[n][0] && row1 > intArray[n][2] && col1 + 1 < intArray[n][1] && col1 + 1 > intArray[n][3])
            return true;
        else
            return false;
    }

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

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