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

package topcoder;

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值