(This is not a complete answer, I'm just sharing what I'm working on so we can collaborate.)
I think that a good first step is to convert the binary grid, giving each cell the value of the maximum size of the square that the cell can be in the upper left corner, for example:
0,0,3,2,1,0,3,2,2,2,2,1,0,0,0,0,0,0,2,1,0,0,0,0,0,2,1,0,0,0 0,0,2,2,2,3,3,2,1,1,1,1,0,0,0,3,3,3,3,3,3,2,1,0,0,1,2,1,0,0 0,2,1,1,1,2,3,2,1,0,0,0,0,3,2,2,2,2,2,2,3,3,2,1,0,0,3,2,1,0 3,2,1,0,0,1,3,2,1,0,0,0,3,2,2,1,1,1,1,1,2,3,3,2,1,0,2,2,2,1 3,3,2,1,0,0,2,2,2,1,0,3,2,2,1,1,0,0,0,0,1,2,4,3,2,2,1,1,1,1 2,3,3,2,1,0,2,1,1,1,2,3,2,1,1,0,0,0,0,0,0,1,3,3,2,1,1,0,0,0 1,2,3,4,3,2,1,1,0,0,1,3,2,1,0,0,0,0,0,0,0,0,2,2,2,1,0,0,0,0 0,1,2,3,3,2,1,0,0,0,0,2,2,1,0,0,0,0,0,0,0,0,2,1,1,2,2,2,1,0 0,0,1,2,3,2,1,0,0,0,0,1,2,1,0,0,0,0,0,0,0,0,2,1,0,1,1,2,1,0 0,0,0,1,2,2,1,0,0,0,0,0,2,1,0,0,0,0,0,0,0,2,1,1,0,0,0,3,2,1 1,0,0,0,1,2,1,0,0,0,0,0,4,3,2,1,0,0,0,4,3,2,1,0,0,0,0,2,2,1 2,1,0,0,0,1,2,1,0,0,5,5,4,4,4,4,4,4,4,5,5,4,3,2,1,0,0,1,2,1 3,2,1,0,0,0,1,6,6,5,4,4,4,3,3,3,3,3,3,4,4,5,4,3,2,1,0,0,1,1 3,2,1,0,0,0,0,6,5,5,4,3,3,3,2,2,2,2,2,3,3,4,5,4,3,2,1,0,0,0 3,2,2,2,2,7,6,6,5,4,4,3,2,2,2,1,1,1,1,2,2,3,5,5,4,3,2,1,0,0 2,2,1,1,1,7,6,5,5,4,3,3,2,1,1,1,0,0,0,1,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1,0,0,0,0,0,0,0 1,1,1,2,1,0,0,0,0,0,0,0,8,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0 0,0,0,2,1,0,0,0,0,0,0,0,8,8,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0 0,0,0,2,1,0,0,0,0,0,0,6,8,7,7,6,6,5,4,3,2,1,0,0,0,0,0,0,0,0 0,0,0,2,2,2,3,3,3,3,3,5,7,7,6,6,5,5,4,3,3,3,3,2,1,0,0,0,0,0 0,0,3,2,1,1,3,2,2,2,2,4,6,6,6,5,5,4,4,3,2,2,2,2,1,0,0,0,0,0 0,0,3,2,1,0,3,2,1,1,1,3,5,5,5,5,4,4,3,3,2,1,1,2,1,0,0,0,0,0 0,0,3,2,1,0,3,2,1,0,0,2,4,4,4,4,4,3,3,2,2,1,0,2,1,0,0,0,0,0 0,4,3,2,1,0,3,2,1,0,0,1,3,3,3,4,3,3,2,2,1,1,0,2,1,0,0,0,0,0 0,4,3,2,1,0,3,2,1,0,0,0,2,2,2,3,3,2,2,1,1,0,0,2,1,0,0,0,0,0 0,4,3,2,1,0,3,2,1,0,0,0,1,1,1,2,2,2,1,1,0,0,0,2,1,0,0,0,0,0 3,3,3,2,1,0,3,2,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,3,2,1,0,0,0,0 2,2,2,2,1,0,2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,1,0,0,0,0 1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0
If you want to view all the parameters using brute force, try each size of the square, which the cell can be an angle (including 1x1), mark the square with zeros, adjust the cell values โโto 7 places to the left / above the square and recursion with a new grid.
If you iterate through the cells from top to bottom and from left to right, you will only need to copy the grid, starting from the current row to the bottom row, and you will only need to adjust the cell values โโto 7 places to the left of the area.
The JS code I tested quickly runs for the top 2 or 3 rows of the grid (result: 24 and 44), takes 8 seconds to finish the top 4 lines (result: 70) and 30 minutes for 5 lines (result: 86). I do not try 6 lines.
But, as you can see from this grid, the number of possibilities is so great that brute force will never be an option. On the other hand, at first I try something like adding large squares, and then filling the remaining space with smaller squares, I will never guarantee the optimal result, I'm afraid. It is too easy to come up with examples that could interfere with such a strategy.
7,6,5,4,3,2,1,0,0,0,0,0,0,7,6,5,4,3,2,1 6,6,5,4,3,2,1,0,0,0,0,0,0,6,6,5,4,3,2,1 5,5,5,4,3,2,1,0,0,0,0,0,0,5,5,5,4,3,2,1 4,4,4,4,3,2,1,0,0,0,0,0,0,4,4,4,4,3,2,1 3,3,3,3,3,2,1,0,0,0,0,0,0,3,3,3,3,3,2,1 2,2,2,2,2,2,1,0,0,0,0,0,0,2,2,2,2,2,2,1 1,1,1,1,1,1,8,7,6,5,4,3,2,1,1,1,1,1,1,1 0,0,0,0,0,0,7,7,6,5,4,3,2,1,0,0,0,0,0,0 0,0,0,0,0,0,6,6,6,5,4,3,2,1,0,0,0,0,0,0 0,0,0,0,0,0,5,5,5,5,4,3,2,1,0,0,0,0,0,0 0,0,0,0,0,0,4,4,4,4,4,3,2,1,0,0,0,0,0,0 0,0,0,0,0,0,3,3,3,3,3,3,2,1,0,0,0,0,0,0 0,0,0,0,0,0,2,2,2,2,2,2,2,1,0,0,0,0,0,0 7,6,5,4,3,2,1,1,1,1,1,1,1,7,6,5,4,3,2,1 6,6,5,4,3,2,1,0,0,0,0,0,0,6,6,5,4,3,2,1 5,5,5,4,3,2,1,0,0,0,0,0,0,5,5,5,4,3,2,1 4,4,4,4,3,2,1,0,0,0,0,0,0,4,4,4,4,3,2,1 3,3,3,3,3,2,1,0,0,0,0,0,0,3,3,3,3,3,2,1 2,2,2,2,2,2,1,0,0,0,0,0,0,2,2,2,2,2,2,1 1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1
In the above example, placing an 8x8 square in the center and four 6x6 squares in the corners gives a lower score than placing a 6x6 square in the center and four 7x7 squares in the corners; therefore, a greedy approach based on using the maximum possible square will not give an optimal result.
Thatโs how I got the selection of zones connected by corridors with a maximum width of 3, and the brute force algorithm on smaller grids. If the border does not have an orange zone, adding two more cells does not increase the account of the isolated zone, so these cells can be used unconditionally by the main zone.
