-
Notifications
You must be signed in to change notification settings - Fork 1
/
1240.铺瓷砖.cpp
110 lines (96 loc) · 2.2 KB
/
1240.铺瓷砖.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "s.h"
/*
* @lc app=leetcode.cn id=1240 lang=cpp
*
* [1240] 铺瓷砖
*/
// @lc code=start
class Solution {
private:
vector<vector<int>> grid;
int n ,m;
int ans;
void fill(int x, int y ,int len){
for(int i=x;i<x+len;i++){
for(int j=y;j<y+len;j++){
grid[i][j] = 1;
}
}
}
void erase(int x, int y ,int len){
for(int i=x;i<x+len;i++){
for(int j=y;j<y+len;j++){
grid[i][j] = 0;
}
}
}
void dfs(int x, int y, int cnt){
bool full=1;
if(cnt>=ans){
return;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==0) {
full=0;
break;
}
}
if(!full){
break;
}
}
if(full){
ans = min(ans, cnt);
}
int ans = INT_MAX;
int si=0,sj=0;
for(int i=0;i<n;i++){
bool found = 0;
for(int j=0;j<m;j++){
if(grid[i][j]==0){
si=i;
sj=j;
found=1;
break;
}
}
if(found){
break;
}
}
for(int l=1;l<=min(n-si,m-sj);l++){
bool can=1;
for(int i1=si;i1<l+si;i1++){
for(int j1= sj;j1<l+sj;j1++){
if(grid[i1][j1]==1){
can=0;
break;
}
}
if(can==0){
break;
}
}
if(can){
fill(si, sj, l);
dfs(si,sj, cnt+1);
erase(si, sj ,l);
}
}
}
public:
int tilingRectangle(int _n, int _m) {
n = _n;
m = _m;
ans = max(n,m);
grid = vector<vector<int>>(_n, vector<int>(_m));
dfs(0,0, 0);
return ans;
}
};
// @lc code=end
int main(){
Solution s;
cout << s.tilingRectangle(11,13);
}