题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4529

郑厂长系列故事——N骑士问题

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 642    Accepted Submission(s): 315

Problem Description
  郑厂长不是正厂长
  也不是副厂长
  他根本就不是厂长
  还是那个腾讯公司的码农
  一个业余时间喜欢下棋的码农
  
  最近,郑厂长对八皇后问题很感兴趣,拿着国际象棋研究了好几天,终于研究透了。兴奋之余,坐在棋盘前的他又开始无聊了。无意间,他看见眼前的棋盘上只摆了八个皇后,感觉空荡荡的,恰好又发现身边还有几个骑士,于是,他想把这些骑士也摆到棋盘上去,当然棋盘上的一个位置只能放一个棋子。因为受八皇后问题的影响,他希望自己把这些骑士摆上去之后,也要满足每2个骑士之间不能相互攻击。
  现在郑厂长想知道共有多少种摆法,你能帮助他吗?

骑士的下法:
  每步棋先横走或直走一格,然后再往外斜走一格;或者先斜走一格,最后再往外横走或竖走一格(即走“日”字)。可以越子,没有"中国象棋"的"蹩马腿"限制。

 
Input
输入第一行为一个整数T(1<=T<=8),表示有T组测试数据;
每组数据首先是一个整数N(1<=n<=10),表示要摆N个骑士上去;
接下来是一个8*8的矩阵来描述一个棋盘,’.’表示这个位置是空的,’*’表示这个位置上已经放了皇后了;
数据中的初始棋盘保证是一个合法的八皇后摆法。
 
Output
对每组数据,请在一行内输出一个整数,表示合法的方案数。
 
Sample Input
2
1
*.......
....*...
.......*
.....*..
..*.....
......*.
.*......
...*....
2
*.......
....*...
.......*
.....*..
..*.....
......*.
.*......
...*....
 
Sample Output
56
1409
 
Source

题解:

1.与此题(POJ1185 炮兵阵地)类似。

2.设dp[i][j][s1][s2]为:到第i行,总共放了j个,第i-1行的状态为s1,第i行的状态为s2的情况数。

3.复杂度为8*10*(1<<8)*(1<<8)*(1<<8),理论上是会超时的,但因为有很多剪枝,所以可以把时间复杂度降下去。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = <<; int row[];
int cnt[];
int dp[][][MAXN][MAXN]; int main()
{
int T, n;
char str[];
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=; i++)
{
scanf("%s", str);
row[i] = ;
for(int j = ; j<; j++)
row[i] += (str[j]=='*')*(<<j);
} for(int s = ; s<MAXN; s++)
{
cnt[s] = ;
for(int j = ; j<; j++)
if(s&(<<j))
cnt[s]++;
} memset(dp, , sizeof(dp));
dp[][][][] = ;
for(int i = ; i<=; i++)
{
for(int j = ; j<=n; j++)
{
for(int s1 = ; s1<MAXN; s1++)
{
if(i>&&(s1&row[i-])) continue;
if(cnt[s1]>n) continue;
for(int s2 = ; s2<MAXN; s2++)
{
if(i>&&(s2&row[i-])) continue;
if( (s1&(s2<<)) || (s1&(s2>>)) ) continue;
if(cnt[s2]>n) continue;
for(int s3 = ; s3<MAXN; s3++)
{
if(s3&row[i]) continue;
if( (s1&(s3<<)) || (s1&(s3>>)) ) continue;
if( (s2&(s3<<)) || (s2&(s3>>)) ) continue;
if(j-cnt[s3]<) continue;
dp[i][j][s2][s3] += dp[i-][j-cnt[s3]][s1][s2];
}
}
}
}
} LL ans = ;
for(int s1 = ; s1<MAXN; s1++)
for(int s2 = ; s2<MAXN; s2++)
ans += dp[][n][s1][s2];
printf("%lld\n", ans);
}
}

滚动数组:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = <<; int row[];
int cnt[];
int dp[][][MAXN][MAXN]; int main()
{
int T, n;
char str[];
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=; i++)
{
scanf("%s", str);
row[i] = ;
for(int j = ; j<; j++)
row[i] += (str[j]=='*')*(<<j);
} for(int s = ; s<MAXN; s++)
{
cnt[s] = ;
for(int j = ; j<; j++)
if(s&(<<j))
cnt[s]++;
} memset(dp[], , sizeof(dp[]));
dp[][][][] = ;
for(int i = ; i<=; i++)
{
memset(dp[i%], , sizeof(dp[i%]));
for(int j = ; j<=n; j++)
{
for(int s1 = ; s1<MAXN; s1++)
{
if(i>&&(s1&row[i-])) continue;
if(cnt[s1]>n) continue;
for(int s2 = ; s2<MAXN; s2++)
{
if(i>&&(s2&row[i-])) continue;
if( (s1&(s2<<)) || (s1&(s2>>)) ) continue;
if(cnt[s2]>n) continue;
for(int s3 = ; s3<MAXN; s3++)
{
if(s3&row[i]) continue;
if( (s1&(s3<<)) || (s1&(s3>>)) ) continue;
if( (s2&(s3<<)) || (s2&(s3>>)) ) continue;
if(j-cnt[s3]<) continue;
dp[i%][j][s2][s3] += dp[(i+)%][j-cnt[s3]][s1][s2];
}
}
}
}
} LL ans = ;
for(int s1 = ; s1<MAXN; s1++)
for(int s2 = ; s2<MAXN; s2++)
ans += dp[][n][s1][s2];
printf("%lld\n", ans);
}
}

HDU4529 郑厂长系列故事——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. hdu_4529_郑厂长系列故事——N骑士问题(状压DP)

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

  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

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  6. hdu_4539_郑厂长系列故事——排兵布阵(状压DP|最大团)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 题意:中文,不解释 题解:将每一行的状态压缩,然后进行DP,也可以用最大团做.这里我用的DP # ...

  7. HDU----(4519)郑厂长系列故事——体检

    郑厂长系列故事——体检 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

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

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

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

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

随机推荐

  1. 2017.2.12 开涛shiro教程-第八章-拦截器机制

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 1.拦截器介绍 下图是shiro拦截器的基础类图: 1.Namea ...

  2. HTML5 Canvas 绘制澳大利亚国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  3. HTML5 Canvas 绘制星条旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  4. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

  5. zoj3329--One Person Game(概率dp第六弹:形成环的dp,带入系数,高斯消元)

    One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very ...

  6. 国内云引擎平台概览——新浪SAE,阿里ACE,百度BCE

    新浪SAE 平时大家的測试server都是执行在自己的PC上面,用Tomcat或者IIS搭建的本机server. 事实上新浪云平台SinaAppEngine也是挺好用的. 今天总结一下我使用过程中的一 ...

  7. redis实现訪问频次限制的几种方式

    结合上一篇文章<redis在学生抢房应用中的实践小结>中提及的用redis实现DDOS设计时遇到的expire的坑.事实上,redis官网中对incr命令的介绍中已经有关于怎样用redis ...

  8. Ubuntu14.04 x86_64 install Xen

    Recommended reference: https://help.ubuntu.com/community/Xen Step One: Install Ubuntu14.04 on your c ...

  9. webdriver.py--解说

    一.全局操作类 start_session 使用指定的desired capabilities创建一个会话(session)start_client 新建一个webdriver会话session前调用 ...

  10. 为备考二级C语言做的代码练习---辅导资料《C语言经典编程282例》--(1)

    因为二级考试的时候用的C语言编译器是VC++6.0 真是日了狗了 用这个编译器 这是我第2个C编译器吧,第一个用的是啊哈C编译器..第二个是VS++6.0 然后在win下用VS2013感觉挺不错的 毕 ...