题目描述
Given a rectangle of size n x m, find the minimum number of integer-sided squares that tile the rectangle.
Example 1:
Input: n = 2, m = 3
Output: 3
Explanation: 3 squares are necessary to cover the rectangle.
2 (squares of 1x1)
1 (square of 2x2)
Example 2:
Input: n = 5, m = 8
Output: 5
Example 3:
Input: n = 11, m = 13
Output: 6
Constraints:
1 <= n <= 13
1 <= m <= 13
思路
DFS,每次从最低的位置开始尝试放所有能放的正方形,边长从大到小尝试。
代码
class Solution {
public:
int tilingRectangle(int n, int m) {
vector<int> h(m, 0);
dfs(n, m, h, 0)<