How to draw rectangles for 2D arrays

I have an 2D boolean array that looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]

(0 = false, 1 = true)

I’ve found a way to combine neighbouring zeros on the horizontal plane like like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[----------------------------]
[----------------------------]
[----------------------------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[----------------------------]
[----------------------------]
</code>
<code>[----------------------------] [----------------------------] [----------------------------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [----------------------------] [----------------------------] </code>
[----------------------------]
[----------------------------]
[----------------------------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[----------------------------]
[----------------------------]

The code for achieving this is as such:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for y = 0 to ySize
for x = 0 to xSize
if array[y][x] = 0
set width = 1
for x2 = x + 1 to xSize
if array[y][x2] = 0
width++
else
break
add object at x, y and set width to width
set x = x + width - 1
</code>
<code>for y = 0 to ySize for x = 0 to xSize if array[y][x] = 0 set width = 1 for x2 = x + 1 to xSize if array[y][x2] = 0 width++ else break add object at x, y and set width to width set x = x + width - 1 </code>
for y = 0 to ySize
    for x = 0 to xSize
        if array[y][x] = 0
            set width = 1

            for x2 = x + 1 to xSize
                if array[y][x2] = 0
                    width++
                else
                    break

            add object at x, y and set width to width
            set x = x + width - 1

So it loops the 2D array looking for zeros, and if it finds one, it loops until the end of the row to find where it is no longer a zero.

It then creates an object at the position where it first found a zero, and stretches it to fill the part until it is not a zero.

I want to adapt this code in order to make rectangles instead of long planks, like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ____________________________
| |
| | <------ rect at 0, 0 with size (10, 3)
|____________________________|
| |[1][1][1]| |
| |[1][1][1]| |
| |[1][1][1]| | <------ rect at 6, 3 with size (4, 5)
| |[1][1][1]| |
| |[1][1][1]|__________|
| | |
|_______|____________________| <------ rect at 3, 8 with size (7, 2)
^
|
|
rect at 0, 3 with size (3, 7)
</code>
<code> ____________________________ | | | | <------ rect at 0, 0 with size (10, 3) |____________________________| | |[1][1][1]| | | |[1][1][1]| | | |[1][1][1]| | <------ rect at 6, 3 with size (4, 5) | |[1][1][1]| | | |[1][1][1]|__________| | | | |_______|____________________| <------ rect at 3, 8 with size (7, 2) ^ | | rect at 0, 3 with size (3, 7) </code>
 ____________________________
|                            |
|                            |   <------ rect at 0, 0 with size (10, 3)
|____________________________|
|       |[1][1][1]|          |
|       |[1][1][1]|          |
|       |[1][1][1]|          |   <------ rect at 6, 3 with size (4, 5)
|       |[1][1][1]|          |
|       |[1][1][1]|__________|
|       |                    |
|_______|____________________|   <------ rect at 3, 8 with size (7, 2)

   ^
   |
   |
  rect at 0, 3 with size (3, 7)

So instead of making 15 horizontal planks, it makes 4 rectangles that surround the ones.

In short, I want to create an algorithm that will, when given an 2D array of ones and zeros, create rectangles around the ones.

For a smaller example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> {
[0][0][0] { 0, 0, 0 },
[0][1][0] { 0, 1, 0 },
[0][0][0] { 0, 0, 0 }
}
</code>
<code> { [0][0][0] { 0, 0, 0 }, [0][1][0] { 0, 1, 0 }, [0][0][0] { 0, 0, 0 } } </code>
             {
[0][0][0]         { 0, 0, 0 },
[0][1][0]         { 0, 1, 0 },
[0][0][0]         { 0, 0, 0 }
             }

Will return the position and dimension of rectangles such as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[-------]
[-][1][-]
[-------]
{
{0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1
{0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1
{2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1
{0, 2, 3, 1} //rect at (0, 2) with width 3, height 1
}
</code>
<code>[-------] [-][1][-] [-------] { {0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1 {0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1 {2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1 {0, 2, 3, 1} //rect at (0, 2) with width 3, height 1 } </code>
[-------]  
[-][1][-]
[-------]

{
    {0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1
    {0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1
    {2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1
    {0, 2, 3, 1}  //rect at (0, 2) with width 3, height 1
}

What should this algorithm look like?

Edit: There should be some effort to minimize number of rectangles used (otherwise, it could just leave it as is).

Also, it doesn’t matter if it prefers horizontal or vertical rectangles. Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[-------]
[-][1][-]
[-------]
</code>
<code>[-------] [-][1][-] [-------] </code>
[-------]
[-][1][-]
[-------]

is the same as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> _ _
| |[ ]| |
| |[1]| |
|_|[ ]|_|
</code>
<code> _ _ | |[ ]| | | |[1]| | |_|[ ]|_| </code>
 _     _
| |[ ]| |
| |[1]| |
|_|[ ]|_|

In the end, both have 4 rectangles surrounding the [1].

4

I think this is a not so easy problem, when handling more complex arrays, such as :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][1][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][1][0][0][0][0][0] [0][0][0][0][0][0][0][1][0][0] [0][0][0][0][0][1][0][0][1][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][1][0][0][0] [0][0][0][0][1][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][1][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0]       
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0]

The real complexity lies in the attempt to use the least possible rectangles. One approach would be to calculate the column of the first ‘1’ appearing in each row. Then, the minimum column is used to create a subarray. For instance :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][1][0][0][0] (6) |--------|0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0] (7) | |0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0] (5) -> min=3 -> | |0][0][1][0][0][1][0] --> ...
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0] (6) | |0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0] (3) ---------|1][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][1][0][0][0] (6) |--------|0][0][0][1][0][0][0] [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0] [0][0][0][0][0][0][0][1][0][0] (7) | |0][0][0][0][1][0][0] [0][0][0][0][0][1][0][0][1][0] (5) -> min=3 -> | |0][0][1][0][0][1][0] --> ... [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][0][0][0][1][0][0][0] (6) | |0][0][0][1][0][0][0] [0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][1][0][0][0][0][0][0] (3) ---------|1][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][1][0][0][0] (6)              |--------|0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4)              |        |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0] (7)              |        |0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0] (5) -> min=3 ->  |        |0][0][1][0][0][1][0] --> ...
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0] (6)              |        |0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4)              |        |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0] (3)              ---------|1][0][0][0][0][0][0]

Then continue iterating from the 1st row. However, I am not sure if this solution finds the smallest number of rectangles.

Another approach would be to start from the point that is the nearest to the center of the 2D array and execute a BFS algorithm, until you find a ‘1’. Then, this BFS will have created a rectangle and then the next iteration will be executed for the new point that is the nearest to the center. This approach is for sure sub-optimal, since a lot of small rectangles will be created in corners.

I could see this being a recursive function in which you seek the top-left corner of the next possible rectangle. Scan right and down, keeping track of the new rectangle width and height to determine the length to check in your next scan row/column. When you’ve hit on both left edge and bottom edges, add result to result list, mark those “cells” as used, and reiterate. When you find no empty cells, you’re done.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How to draw rectangles for 2D arrays

I have an 2D boolean array that looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][1][1][1][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]

(0 = false, 1 = true)

I’ve found a way to combine neighbouring zeros on the horizontal plane like like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[----------------------------]
[----------------------------]
[----------------------------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[----------------------------]
[----------------------------]
</code>
<code>[----------------------------] [----------------------------] [----------------------------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [-------][1][1][1][----------] [----------------------------] [----------------------------] </code>
[----------------------------]
[----------------------------]
[----------------------------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[----------------------------]
[----------------------------]

The code for achieving this is as such:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for y = 0 to ySize
for x = 0 to xSize
if array[y][x] = 0
set width = 1
for x2 = x + 1 to xSize
if array[y][x2] = 0
width++
else
break
add object at x, y and set width to width
set x = x + width - 1
</code>
<code>for y = 0 to ySize for x = 0 to xSize if array[y][x] = 0 set width = 1 for x2 = x + 1 to xSize if array[y][x2] = 0 width++ else break add object at x, y and set width to width set x = x + width - 1 </code>
for y = 0 to ySize
    for x = 0 to xSize
        if array[y][x] = 0
            set width = 1

            for x2 = x + 1 to xSize
                if array[y][x2] = 0
                    width++
                else
                    break

            add object at x, y and set width to width
            set x = x + width - 1

So it loops the 2D array looking for zeros, and if it finds one, it loops until the end of the row to find where it is no longer a zero.

It then creates an object at the position where it first found a zero, and stretches it to fill the part until it is not a zero.

I want to adapt this code in order to make rectangles instead of long planks, like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ____________________________
| |
| | <------ rect at 0, 0 with size (10, 3)
|____________________________|
| |[1][1][1]| |
| |[1][1][1]| |
| |[1][1][1]| | <------ rect at 6, 3 with size (4, 5)
| |[1][1][1]| |
| |[1][1][1]|__________|
| | |
|_______|____________________| <------ rect at 3, 8 with size (7, 2)
^
|
|
rect at 0, 3 with size (3, 7)
</code>
<code> ____________________________ | | | | <------ rect at 0, 0 with size (10, 3) |____________________________| | |[1][1][1]| | | |[1][1][1]| | | |[1][1][1]| | <------ rect at 6, 3 with size (4, 5) | |[1][1][1]| | | |[1][1][1]|__________| | | | |_______|____________________| <------ rect at 3, 8 with size (7, 2) ^ | | rect at 0, 3 with size (3, 7) </code>
 ____________________________
|                            |
|                            |   <------ rect at 0, 0 with size (10, 3)
|____________________________|
|       |[1][1][1]|          |
|       |[1][1][1]|          |
|       |[1][1][1]|          |   <------ rect at 6, 3 with size (4, 5)
|       |[1][1][1]|          |
|       |[1][1][1]|__________|
|       |                    |
|_______|____________________|   <------ rect at 3, 8 with size (7, 2)

   ^
   |
   |
  rect at 0, 3 with size (3, 7)

So instead of making 15 horizontal planks, it makes 4 rectangles that surround the ones.

In short, I want to create an algorithm that will, when given an 2D array of ones and zeros, create rectangles around the ones.

For a smaller example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> {
[0][0][0] { 0, 0, 0 },
[0][1][0] { 0, 1, 0 },
[0][0][0] { 0, 0, 0 }
}
</code>
<code> { [0][0][0] { 0, 0, 0 }, [0][1][0] { 0, 1, 0 }, [0][0][0] { 0, 0, 0 } } </code>
             {
[0][0][0]         { 0, 0, 0 },
[0][1][0]         { 0, 1, 0 },
[0][0][0]         { 0, 0, 0 }
             }

Will return the position and dimension of rectangles such as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[-------]
[-][1][-]
[-------]
{
{0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1
{0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1
{2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1
{0, 2, 3, 1} //rect at (0, 2) with width 3, height 1
}
</code>
<code>[-------] [-][1][-] [-------] { {0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1 {0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1 {2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1 {0, 2, 3, 1} //rect at (0, 2) with width 3, height 1 } </code>
[-------]  
[-][1][-]
[-------]

{
    {0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1
    {0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1
    {2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1
    {0, 2, 3, 1}  //rect at (0, 2) with width 3, height 1
}

What should this algorithm look like?

Edit: There should be some effort to minimize number of rectangles used (otherwise, it could just leave it as is).

Also, it doesn’t matter if it prefers horizontal or vertical rectangles. Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[-------]
[-][1][-]
[-------]
</code>
<code>[-------] [-][1][-] [-------] </code>
[-------]
[-][1][-]
[-------]

is the same as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> _ _
| |[ ]| |
| |[1]| |
|_|[ ]|_|
</code>
<code> _ _ | |[ ]| | | |[1]| | |_|[ ]|_| </code>
 _     _
| |[ ]| |
| |[1]| |
|_|[ ]|_|

In the end, both have 4 rectangles surrounding the [1].

4

I think this is a not so easy problem, when handling more complex arrays, such as :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][1][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][1][0][0][0][0][0] [0][0][0][0][0][0][0][1][0][0] [0][0][0][0][0][1][0][0][1][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][0][0][0][1][0][0][0] [0][0][0][0][1][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] [0][0][0][1][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0]       
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0]

The real complexity lies in the attempt to use the least possible rectangles. One approach would be to calculate the column of the first ‘1’ appearing in each row. Then, the minimum column is used to create a subarray. For instance :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[0][0][0][0][0][0][1][0][0][0] (6) |--------|0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0] (7) | |0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0] (5) -> min=3 -> | |0][0][1][0][0][1][0] --> ...
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0] (6) | |0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0] (3) ---------|1][0][0][0][0][0][0]
</code>
<code>[0][0][0][0][0][0][1][0][0][0] (6) |--------|0][0][0][1][0][0][0] [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0] [0][0][0][0][0][0][0][1][0][0] (7) | |0][0][0][0][1][0][0] [0][0][0][0][0][1][0][0][1][0] (5) -> min=3 -> | |0][0][1][0][0][1][0] --> ... [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][0][0][0][1][0][0][0] (6) | |0][0][0][1][0][0][0] [0][0][0][0][1][0][0][0][0][0] (4) | |0][1][0][0][0][0][0] [0][0][0][0][0][0][0][0][0][0] (-1) | |0][0][0][0][0][0][0] [0][0][0][1][0][0][0][0][0][0] (3) ---------|1][0][0][0][0][0][0] </code>
[0][0][0][0][0][0][1][0][0][0] (6)              |--------|0][0][0][1][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4)              |        |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][1][0][0] (7)              |        |0][0][0][0][1][0][0]
[0][0][0][0][0][1][0][0][1][0] (5) -> min=3 ->  |        |0][0][1][0][0][1][0] --> ...
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][0][0][0][1][0][0][0] (6)              |        |0][0][0][1][0][0][0]
[0][0][0][0][1][0][0][0][0][0] (4)              |        |0][1][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0] (-1)             |        |0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0][0][0] (3)              ---------|1][0][0][0][0][0][0]

Then continue iterating from the 1st row. However, I am not sure if this solution finds the smallest number of rectangles.

Another approach would be to start from the point that is the nearest to the center of the 2D array and execute a BFS algorithm, until you find a ‘1’. Then, this BFS will have created a rectangle and then the next iteration will be executed for the new point that is the nearest to the center. This approach is for sure sub-optimal, since a lot of small rectangles will be created in corners.

I could see this being a recursive function in which you seek the top-left corner of the next possible rectangle. Scan right and down, keeping track of the new rectangle width and height to determine the length to check in your next scan row/column. When you’ve hit on both left edge and bottom edges, add result to result list, mark those “cells” as used, and reiterate. When you find no empty cells, you’re done.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật