一个大矩形,要你分割成小矩形(只能水平和竖直切割),每个小矩形都有一个价值,问如何分割能得到最大总价值。
思路
代码
#include "bits/stdc++.h"
const int N = 1111;
using namespace std;
int f[N][N];
int main() {
int t;
cin >> t;
while (t--) {
int n, X, Y;
cin >> n >> X >> Y;
memset(f, 0, sizeof f);
vector<int> dx(n + 1), dy(n + 1), val(n + 1);
for (int i = 1; i <= n; ++i)
cin >> dx[i] >> dy[i] >> val[i];
for (int i = 0; i <= X; ++i)
for (int j = 0; j <= Y; ++j)
for (int k = 1; k <= n; ++k) {
int x = dx[k], y = dy[k], v = val[k];
if (i >= x && j >= y) {
f[i][j] = max (f[i][j], f[i - x][j] + f[x][j - y] + v);
f[i][j] = max (f[i][j], f[i - x][y] + f[i][j - y] + v);
}
if (i >= y && j >= x) {
f[i][j] = max (f[i][j], f[i - y][j] + f[y][j - x] + v);
f[i][j] = max (f[i][j], f[i - y][x] + f[i][j - x] + v);
}
}
cout << f[X][Y] << endl;
}
return 0;
}
|