给定高为m、宽为n的木块,同时给出prices[i]={h[i],w[i],price[i]}
,表示高为h[i]、宽为w[i]的木块可以卖得price[i]的钱。切割木块时只能水平或垂直一切到底,木块不能旋转,切割次数不限,求最多能卖多少钱。
1<=m,n<=200; 1<=prices.length<=2e4; 1<=h[i]<=m; 1<=w[i]<=n; 1<=price[i]<=1e6; (h[i],w[i])各不相同。
动态规则,从小到大计算,每次枚举切割方向和切割位置,进行递推。
class Solution {
public:
long long p[205][205], dp[205][205];
long long sellingWood(int m, int n, vector<vector<int>>& prices) {
for (auto &i : prices) {
p[i[0]][i[1]] = i[2];
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = p[i][j];
for (int k = 1; k < i; k++) {
dp[i][j] = max(dp[i][j], dp[k][j]+dp[i-k][j]);
}
for (int k = 1; k < j; k++) {
dp[i][j] = max(dp[i][j], dp[i][k]+dp[i][j-k]);
}
}
}
return dp[m][n];
}
};
标签:205,切割,int,木块,long,木头,prices,lc2312
From: https://www.cnblogs.com/chenfy27/p/18091621