ILP Part 93 — Squared Poland

This is the ninetieth third part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve Polska kwadratowa. We have a map of Poland divided into unit squares. We need to cover the map with as few bigger squares as possible. Squares cannot intersect and there are some cities which cannot belong to any bigger square (they form a unit square on their own).


First, we define a board in lines 1-23. Space indicates an empty cell, capital X indicates a free unit square. Hash # indicates a city which can’t belong to any other big square.

We first calculate which fields are valid, e.g., they belong to the country and are free to be covered with a bigger square. That’s in line 25.

Now, the general idea is that for each cell we define whether it’s a top-left corner of some square and how big the square is. To calculate that we need to first precalculate the size of a biggest square we can start in a given cell. We do that in lines 27-60. What we do is we start from some cell and then try to extend it to the right and to the bottom as far as possible.

Now, we start defining variables. First, indicators whether a given cell is a top-left corner of a square (line 62) or if it’s a part of some other square (so it’s dependent) in line 63. Next, we define the id of the square the cell belongs to (line 64) and the square size (lines 65-70).

And now comes the heart of the algorithm. We iterate over all cells and make sure that a cell is either a top-left corner or is dependent (line 75). Notice that cells outside of the country will be considered a top-left ones (this makes the algorithm a little easier).

Next, if a cell is dependent then the size of the square starting in this cell must be zero (as there is no square) – lines 77-83.

And now we iterate over a square starting in this cell (from line 85). We try to move right and down (hence x and y in lines 85 and 86). We check if given cell can be a part of the square — that’s the case if it’s not further away then the maximum possible square size starting in the top left cell (line 89) and if its less or equal to the actual size of the square we want to build start from the given cell. If that’s the case then we mark the other cell as dependent (lines 91-97).

It seems like that should be enough. However, with such an approach the solver would make all cells dependent and call it a day. So we need to actually make sure that the map is covered. In lines 111-122 we take all top-left corners and calculate the area they cover (based on the square size started from such a cell). Next, we minimize the number of top-left corners as we want to have as few squares as possible (line 124-125).

However, that’s not enough as we don’t disallow for intersecting squares. That’s why we need to keep a track of ownership of each dependent cell. We basically add an id of a top-left corner owning the cell to the cell (lines 99-105).

That’s it.