题目链接: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. 代码篇之AOP框架

    AopFrameworkTest类 public class AopFrameworkTest { public static void main(String[] args) throws Exce ...

  2. zabbix学习系列之触发器

    触发器的简介 监控项仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送告警信息,"触发器"正式 用于为监控项所收集的数据定义阈值 每一个触发器 ...

  3. 【温故知新】——BABYLON.js学习之路·前辈经验(二)

    前言:在上一篇随笔BABYLON.js学习之路·前辈经验(一)中回顾了组内同事们长时间在Babylon开发实践中的总结出的学习之路和经验,这一篇主要对开发中常见的一些功能点做一个梳理,这里只作为温故知 ...

  4. java监控工具jstatd-windows

    Monitors Java Virtual Machines (JVMs) and enables remote monitoring tools to attach to JVMs. This co ...

  5. vue v-for与v-if组合使用

    当它们处于同一节点,v-for 的优先级比 v-if 更高,这意味着 v-if 将分别重复运行于每个 v-for 循环中.当你想为仅有的_一些_项渲染节点时,这种优先级的机制会十分有用,如下: < ...

  6. 谷歌浏览器插件-html页面js事件查看器

    谷歌浏览器插件-html页面js事件查看器 1.下载 下载地址:http://files.cnblogs.com/files/graceup/VisualEvent.zip 解压得到文件:Visual ...

  7. leetcode-Symmetric Tree 对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. 利用动态图添加Loading动画

    opacity:CSS3中的属性,调节透明度,一般取值0.5 添加思想: 1.对超链接添加点击事件,通过new {@onclick="showLoading()"} Html.Ac ...

  9. c#中使用ABCpdf处理PDF,so easy

    QQ交流群:276874828  (ABCpdf ) 这几天项目中需要将页面导成PDF,刚开始使用iTextSharp,觉得在分页处理上比较复杂,后来无意中看到了ABCpdf,使用非常简单,并将一些常 ...

  10. JavaScript中的ajax(二)

    一.Ajax概念Ajax是(Asynchronous JavaScript And XML)是异步的JavaScript和xml.也就是异步请求更新技术.Ajax是一种对现有技术的一种新的应用,不是一 ...