2D数组到矩形

时间:2014-04-04 20:52:47

标签: java arrays algorithm multidimensional-array

有没有办法将像这样的二维数组解析为矩形对象(x,y,width,height)?我需要所有可能的矩形数组......

{0,0,0,0,0}
{0,0,0,0,0}
{0,1,1,0,0}
{0,1,1,0,0}
{0,0,0,0,0}

这会产生4个矩形(我们看0):

0,0,5,2
0,0,1,5
3,0,2,5
0,5,5,1

我尝试过类似的东西,但它只给出了最大矩形的区域......

public static int[] findMaxRectangleArea(int[][] A, int m, int n) {
    // m=rows & n=cols according to question
    int corX =0, corY = 0;
    int[] single = new int[n];
    int largeX = 0, largest = 0;
    for (int i = 0; i < m; i++) {
        single = new int[n]; // one d array used to check line by line &
                                // it's size will be n
        for (int k = i; k < m; k++) { // this is used for to run until i
                                        // contains element
            int a = 0;
            int y = k - i + 1; // is used for row and col of the comming
                                // array
            int shrt = 0, ii = 0, small = 0;
            int mix = 0;
            int findX = 0;
            for (int j = 0; j < n; j++) {
                single[j] = single[j] + A[k][j]; // postions element are
                                                    // added



                if (single[j] == y) { // element position equals
                    shrt = (a == 0) ? j : shrt; // shortcut
                    a = a + 1;
                    if (a > findX) {
                        findX = a;
                        mix = shrt;



                    }
                } else {
                    a = 0;
                }
            }
            a = findX;

            a = (a == y) ? a - 1 : a;
            if (a * y > largeX * largest) { // here i am checking the values
                                            // with xy
                largeX = a;
                largest = y;
                ii = i;
                small = mix;



            }

        }

    }// end of loop
    return largeX * largest;

}

此代码正在使用1s,但现在不是重点

0 个答案:

没有答案