本题过于经典……
对于这种网格状压DP,套路一波刷表法DFS转移就没了。
三进制状压,0表示当前,上一个都没有。1表示当前无,上一个有。2表示当前有。
转移的条件就是上一行为0,当前不是山地,且左边两个都不是2。
注意有个坑点,全部转移会超时。因为本题有很多废状态(山地),初始化-1然后判断是否转移即可。
#include <cstdio>
#include <algorithm>
#include <cstring> const int N = , M = ; int n, m, f[N][], pre[M], now[M], ans, G[N][M];
char str[M]; inline int zip(int *a) {
int t = ;
for(int i = ; i < m; i++) {
t = t * + a[i];
}
return t;
} inline void unzip(int x, int *a) {
for(int i = m - ; i >= ; i--) {
a[i] = x % ;
x /= ;
}
return;
} void DFS(int x, int y, int lastans) {
if(y >= m) {
int s = zip(now);
f[x][s] = std::max(f[x][s], lastans);
ans = std::max(ans, lastans);
return;
}
DFS(x, y + , lastans);
if(!G[x][y] && pre[y] == && (y < || now[y - ] < ) && (y < || now[y - ] < )) {
now[y] = ;
DFS(x, y + , lastans + );
now[y] = ;
}
return;
} int main() {
memset(f, -, sizeof(f));
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%s", str);
for(int j = ; j < m; j++) {
G[i][j] = (str[j] == 'H');
}
} int lm = ;
for(int i = ; i <= m; i++) {
lm *= ;
}
f[][] = ;
for(int i = ; i < n; i++) {
for(int s = ; s < lm; s++) {
if(f[i][s] == -) {
continue;
}
unzip(s, pre);
for(int j = ; j < m; j++) {
now[j] = std::max(, pre[j] - );
}
DFS(i + , , f[i][s]);
}
} printf("%d", ans);
return ;
}
AC代码
我数组一开始开了2^m个……应该是3^m。