0

Is there a way to parse 2 dimensional array like this into a rectangle object (x,y, width, height)?. I need the array of all possible rectangles...

{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}

This would give 4 rectangles (we are looking at 0):

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

I have tried something like this, but it only gives the area of the biggest rectangle...

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;

}

this code is working with 1s, but that is not the point right now

Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
user1176999
  • 440
  • 8
  • 20
  • 4
    The result is rather arbitrary. Why this particular rectangles? They overlap, and one can find four other non-overlapping rectangles in this example. Depending whether you want to minimize the number of rectangles and overlap, this could be a variant of the set cover problem and thus be NP hard. Either way, you should add some details there. – Zeta Apr 04 '14 at 21:07
  • 2
    May be this is what you want: ["Algorithm for finding the fewest rectangles to cover a set of rectangles"](http://stackoverflow.com/a/6634668/1009831)? – Evgeny Kluev Apr 04 '14 at 21:09
  • I need all possible rectangles... Sorry for not pointing that out... – user1176999 Apr 05 '14 at 08:10
  • 1
    Does that include the 21 rectangles of size 1x1? – Jongware Apr 05 '14 at 11:15

0 Answers0