打砖块(题解)
Withered_sjtu · · 题解
这题一看就是一道
我们可以定义两个辅助数组
最后给出完整 code
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool c[205][205];
char x;
int a[205][205];
int dy[205][405], dn[205][405], dpn[205][405], dpy[205][405];
int main() {
scanf("%d %d %d\n", &n, &m, &k);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j] >> x;
if (x == 'Y') c[i][j] = 1;
}
}
for (int j = 1; j <= m; j++) {
int tot = 0;
for (int i = n; i >= 1; i--) {
if (c[i][j] == 1) dy[j][tot] += a[i][j];
else {
tot++;
dn[j][tot] = dy[j][tot - 1] + a[i][j];
dy[j][tot] = dn[j][tot];
}
}
}
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= k; j++) {
for (int l = 0; l <= min(j, n); l++) {
dpy[i][j] = max(dpy[i][j], dpy[i - 1][j - l] + dy[i][l]);
if (l != 0) dpn[i][j] = max(dpn[i][j], dpy[i - 1][j - l] + dn[i][l]);
if (j != l) dpn[i][j] = max(dpn[i][j], dpn[i - 1][j - l] + dy[i][l]);
}
}
}
printf("%d", dpn[m][k]);
return 0;
}