首页 技术 正文
技术 2022年11月23日
0 收藏 811 点赞 2,504 浏览 1010 个字

本题过于经典……

对于这种网格状压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。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,961
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,485
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,746
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,780