forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiling-a-rectangle-with-the-fewest-squares.cpp
73 lines (68 loc) · 2.16 KB
/
tiling-a-rectangle-with-the-fewest-squares.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Time: O(n^2 * m^2 * m^(n * m)), given m < n
// Space: O(n * m)
class Solution {
public:
int tilingRectangle(int n, int m) {
if (m > n) {
return tilingRectangle(m, n);
}
vector<vector<int>> board(n, vector<int>(m));
int result = numeric_limits<int>::max();
backtracking(&board, 0, &result);
return result;
}
private:
pair<int, int> find_next(const vector<vector<int>>& board) {
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
if (!board[i][j]) {
return {i, j};
}
}
}
return {-1, -1};
}
int find_max_length(const vector<vector<int>>& board, int i, int j) {
int max_length = 1;
while (i + max_length - 1 < board.size() &&
j + max_length - 1 < board[0].size()) {
for (int r = i; r < i + max_length - 1; ++r) {
if (board[r][j + max_length - 1]) {
return max_length - 1;
}
}
for (int c = j; c < j + max_length; ++c) {
if (board[i + max_length - 1][c]) {
return max_length - 1;
}
}
++max_length;
}
return max_length - 1;
}
void fill(vector<vector<int>> *board,
int i, int j, int length, int val) {
for (int r = i; r < i + length; ++r) {
for (int c = j; c < j + length; ++c) {
(*board)[r][c] = val;
}
}
}
void backtracking(vector<vector<int>> *board,
int count, int *result) {
if (count >= *result) { // pruning
return;
}
const auto& [i, j] = find_next(*board);
if (i == -1 && j == -1) { // finished
*result = min(*result, count);
return;
}
const auto& max_length = find_max_length(*board, i, j);
for (int k = max_length; k >= 1; --k) {
fill(board, i, j, k, 1);
backtracking(board, count + 1, result);
fill(board, i, j, k, 0);
}
}
};