题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种。

分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快,但是骑士的话就需要对每一个格子分两种情况进行,情况非常的多,搜索肯定是会超时的。状态压缩DP就是另外一个思路的,理论上时间复杂度是8*n*2^24,但是由于限制比较多,也就能够解决了。设dp[i][j][p][q]表示第i-1行压缩后的状态是p,第i行压缩后的状态为q,且之前一共使用了j个骑士的方案数。按照题意递推即可。

#include <cstdio>
#include <cstring>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int LIM = <<;
const int M = ;
int n;
int dp[][M+][LIM][LIM];
// dp[i][j][p][q]表示第i-1行状态为p,第i行状态为q,并且一共使用j个骑士的状态数
int G[M];
int tot[LIM];
char f1[LIM][LIM]; // 相邻两层两个状态之间是否冲突
char f2[LIM][LIM]; // 与上上行两个状态之间是否冲突 void pre() {
for (int i = ; i < LIM; ++i) {
for (int j = ; j < ; ++j) {
if (i & ( << j)) ++tot[i];
}
for (int j = ; j < LIM; ++j) {
if ((i>>)&j || (j>>)&i) f1[i][j] = ;
if ((i>>)&j || (j>>)&i) f2[i][j] = ;
}
}
} void solve() {
int cur = , nxt = ;
memset(dp, , sizeof (dp));
dp[cur][][][] = ;
for (int i = ; i < ; ++i) { // 由dp[i]来推导dp[i+1]
for (int j = ; j <= n; ++j) {
for (int p = ; p < LIM; ++p) {
for (int q = ; q < LIM; ++q) {
if (!dp[cur][j][p][q]) continue;
for (int z = ; z < LIM; ++z) {
if ((z & G[i+]) != z) continue;
if (tot[z] + j > n) continue;
if (i >= && f1[q][z]) continue;
if (i >= && f2[p][z]) continue;
dp[nxt][tot[z]+j][q][z] += dp[cur][j][p][q];
}
}
}
}
memset(dp[cur], , sizeof (dp[cur]));
swap(cur, nxt);
}
int ret = ;
for (int i = ; i < LIM; ++i) {
for (int j = ; j < LIM; ++j) {
ret += dp[cur][n][i][j];
}
}
printf("%d\n", ret);
} int main() {
int T;
char str[];
pre();
scanf("%d", &T);
while (T--) {
memset(G, , sizeof (G));
scanf("%d", &n);
for (int i = ; i <= ; ++i) {
scanf("%s", str);
for (int j = ; j < ; ++j) {
G[i] <<= ;
if (str[j] == '.') G[i] |= ;
}
}
solve();
}
return ;
}

HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP的更多相关文章

  1. HDU 4529 郑厂长系列故事——N骑士问题 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4529 郑厂长系列故事--N骑士问题 Time Limit: 6000/3000 MS (Java/O ...

  2. HDU4529 郑厂长系列故事——N骑士问题 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4529 郑厂长系列故事——N骑士问题 Time Limit: 6000/3000 MS (Java/Ot ...

  3. HDU 4539 郑厂长系列故事——排兵布阵 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事--排兵布阵 Time Limit: 10000/5000 MS (Java/O ...

  4. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...

  5. hdu_4529_郑厂长系列故事——N骑士问题(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4529 题意:中文,不解释 题解:状压DP,dp[i][j][k][s]表示第i行当前用了j个骑士,i- ...

  6. HDU 4539郑厂长系列故事――排兵布阵(状压DP)

    HDU 4539  郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 //#pragma co ...

  7. HDU 4539 郑厂长系列故事——排兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) ...

  8. hdu 4524 郑厂长系列故事——逃离迷宫 小水题

    郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  9. POJ 1185 - 炮兵阵地 & HDU 4539 - 郑厂长系列故事——排兵布阵 - [状压DP]

    印象中这道题好像我曾经肝过,但是没肝出来,现在肝出来了也挺开心的 题目链接:http://poj.org/problem?id=1185 Time Limit: 2000MS Memory Limit ...

随机推荐

  1. Codeigniter 3.0 相关文档 part one

    分页配置项 http://stackoverflow.com/questions/18418900/codeigniter-pagination-config-without-repeating-wi ...

  2. NEC学习 ---- 模块 - 上图下文图文列表

    上图下文图文列表的效果如下图: 可以看到三个红色框中的三中"上图下文的图文列表"; 这里的代码其实没什么问题, 对于这种布局, 其实可以参考我上一篇介绍: NEC学习 ---- 模 ...

  3. http://highscalability.com/blog/2015/5/18/how-mysql-is-able-to-scale-to-200-million-qps-mysql-cluster.html

    http://highscalability.com/blog/2015/5/18/how-mysql-is-able-to-scale-to-200-million-qps-mysql-cluste ...

  4. rabbitmq之partitions

    集群为了保证数据一致性,在同步数据的同时也会通过节点之间的心跳通信来保证对方存活.那如果集群节点通信异常会发生什么,系统如何保障正常提供服务,使用何种策略回复呢? rabbitmq提供的处理脑裂的方法 ...

  5. Theos

    一.安装 1.配置环境变量 (每次 terminal 重新启动需要配置) $ export THEOS=/opt/theos 2.下载 Theos $ sudo git clone git://git ...

  6. 安装php扩展库

    无法加载'pdo_mysql' ,因为需要pdo这个module.PHP Warning: Cannot load module 'pdo_mysql' because required module ...

  7. inittab 分析

    内核初始化后,启动init进程/sbin/init,读取/etc/inittab文件进行初始化. 参考链接 http://wenku.baidu.com/view/5a82b5f67c1cfad619 ...

  8. mysql中OPTIMIZE TABLE的作用

    在使用mysql的时候有时候,可能会发现尽管一张表删除了许多数据,但是这张表表的数据文件和索引文件却奇怪的没有变小.这是因为mysql在删除数据(特别是有Text和BLOB)的时候,会留下许多的数据空 ...

  9. Nginx执行php显示no input file specified的处理方法

    /var/www/nginx-default中放上一份phpinfo.php,使用http://localhost/phpinfo.info 访问,结果报错,显示 “No input file spe ...

  10. echarts入门基础,画柱型图

    注意:一定要自己引入echarts库 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...