2312. 卖木头块https://leetcode.cn/problems/selling-pieces-of-wood/?envType=daily-question&envId=2024-03-15类似切割钢条,只是竖着切完再试试横着切就行了,递推式:
备忘录递归:
class Solution {
public:
long long sellingWood(int m, int n, vector<vector<int>>& prices) {
vector<vector<int>> p(m+1, vector<int>(n+1, 0));
for (auto vec : prices) {
p[vec[0]][vec[1]] = vec[2];
}
vector<vector<long long>> mem(m + 1, vector<long long>(n+1, -1));
function<long long (int, int)> dfs = [&](int x, int y) {
if (mem[x][y] != -1) return mem[x][y];
if (x == 0 || y == 0) return 0ll;
long long res = p[x][y];
for (int i=1; i <= x/2; ++i) {
res = max(dfs(i, y) + dfs(x-i, y), res);
}
for (int j=1; j <= y/2; ++j) {
res = max(dfs(x, j) + dfs(x, y-j), res);
}
mem[x][y] = res;
return res;
};
return dfs(m, n);
}
};