Cut rectangle in minimum number of squares

前端 未结 6 1476
-上瘾入骨i
-上瘾入骨i 2020-12-28 17:29

I\'m trying to solve the following problem:

A rectangular paper sheet of M*N is to be cut down into squares such that:

  1. The paper i
6条回答
  •  余生分开走
    2020-12-28 18:11

    The greedy algorithm is not optimal. On a 6x5 rectangle, it uses a 5x5 square and 5 1x1 squares. The optimal solution uses 2 3x3 squares and 3 2x2 squares.

    To get an optimal solution, use dynamic programming. The brute-force recursive solution tries all possible horizontal and vertical first cuts, recursively cutting the two pieces optimally. By caching (memoizing) the value of the function for each input, we get a polynomial-time dynamic program (O(m n max(m, n))).

提交回复
热议问题