我们每次从大矩形上切下一块,每次切分必须保证是横着一刀两半或竖着一刀两半,且切下来的那一块是小矩形的一种,
求最多能切几块?
求最多能切几块?
3,5,[[3 ,1],[4,1],[2,2],[2,2]]
5
小矩形无方向要求,不可用边角料拼凑小矩形,同种小矩形可以裁剪多个,数据保证0<裁剪出的小矩形个数<=10,0<大矩形长宽,小矩形长宽<=1000且都是整数
class Solution { public: /** * * @param L int整型 给定布料的长 * @param W int整型 给定布料的宽 * @param clothSize int整型vector<vector<>> 依次列举每件衣服所需的长和宽 * @return int整型 */ int dp[1003][1003]={0}; int clothNumber(int L, int W, vector<vector<int> >& clothSize) { if(dp[L][W]!=0) return dp[L][W]; int Max=0; if(L < W) swap(L, W); for(auto &x: clothSize){ int l=max(x[0], x[1]), w=min(x[0], x[1]), t, a, b; if(l<=max(L,W) && w<=min(L,W)){ if(w<=L && l<=W) swap(l,w); t = (L/l) * (W/w); a = clothNumber(L, W%w, clothSize) + clothNumber(L%l, W-W%w, clothSize); b = clothNumber(L%l, W, clothSize) + clothNumber(L-L%l, W%w, clothSize); t += max(a, b); Max = max(Max, t); } } dp[L][W] = Max; return Max; } };
class Solution { public: /** * * @param L int整型 给定布料的长 * @param W int整型 给定布料的宽 * @param clothSize int整型vector<vector<>> 依次列举每件衣服所需的长和宽 * @return int整型 */ int clothNumber(int L, int W, vector<vector<int> >& clothSize) { // write code here vector<vector<int>> dp(L+1, vector<int>(W+1)); vector<int>a; for(int i = 0; i < clothSize.size(); ++i){ int l = clothSize[i][0], w = clothSize[i][1]; a.push_back(l); a.push_back(w); if(l <= L && w <= W) dp[l][w] = 1; if(l <= W && w <= L) dp[w][l] = 1; } sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); for(int i = 1; i <= L; ++i) for(int j = 1; j <= W; ++j){ for(auto k : a) if(k <= j) dp[i][j] = max(dp[i][j], dp[i][k]+dp[i][j-k]);else break; for(auto k : a) if(k <= i) dp[i][j] = max(dp[i][j], dp[i-k][j]+dp[k][j]);else break; } return dp[L][W]; } };