# Cheapest dissection of a grid polygon into rectangles with cost

My problem:

• Dissect a grid polygon into rectangles. (A grid polygon is a rectilinear polygon all of whose vertices have integer coordinates.)

• The rectangles must be taken from a predefined set (which includes a 1×1 rectangle, so the dissection is always possible).

• Each rectangle has a cost which is not directly proportional to its size (a 2×3 rectangle may cost less 'per grid square covered' than a 1×1 rectangle).

• No gaps or overlaps are allowed.

• The optimal solution is the cheapest that covers the area.

My first thoughts were to work out the cheapest 'per square covered' rectangle and fit as many of those into the area as possible, but it seems that might perform poorly if it means filling a lot of spaces with small, expensive pieces.

I realise this is likely NP-hard, so I am probably looking for a heuristic rather than an algorithm. It seems in some ways similar to a bin-packing problem or a knapsack problem, but I can't see how to extend the solutions to these problems. I may just be lacking the right terminology, so if anyone could point me in the right direction to get started, I'd be grateful.

## migrated from stackoverflow.comMar 26 '12 at 19:05

This question came from our site for professional and enthusiast programmers.

• Your solutions sounds like the simplest way to do it. It follows the standard greedy solution pattern - sort, then start going from best to worst. Try just sorting the rectangle list by efficiency (cost / space covered) and going from there. – bob_twinkles Mar 16 '12 at 3:09
• It's normal to use the terms dissection when overlaps are forbidden (as in this case) and cover when overlaps are allowed. I changed the title accordingly. – Gareth Rees Mar 16 '12 at 10:26
• If the set of rectangles were unconstrained, and every rectangle had the same cost, then this answer would explain how to compute the cheapest dissection. – Gareth Rees Mar 16 '12 at 10:30
• Consider simulated annealing for this. – DeepYellow Mar 16 '12 at 20:28
• I'm considering experimenting with this using simulated annealing. Do you have any sample data, and an appropriate cost function? – DeepYellow Mar 17 '12 at 7:51