HDU4529 郑厂长系列故事——N骑士问题 —— 状压DP
题目链接: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
也不是副厂长
他根本就不是厂长
还是那个腾讯公司的码农
一个业余时间喜欢下棋的码农
最近,郑厂长对八皇后问题很感兴趣,拿着国际象棋研究了好几天,终于研究透了。兴奋之余,坐在棋盘前的他又开始无聊了。无意间,他看见眼前的棋盘上只摆了八个皇后,感觉空荡荡的,恰好又发现身边还有几个骑士,于是,他想把这些骑士也摆到棋盘上去,当然棋盘上的一个位置只能放一个棋子。因为受八皇后问题的影响,他希望自己把这些骑士摆上去之后,也要满足每2个骑士之间不能相互攻击。
现在郑厂长想知道共有多少种摆法,你能帮助他吗?
骑士的下法:
  每步棋先横走或直走一格,然后再往外斜走一格;或者先斜走一格,最后再往外横走或竖走一格(即走“日”字)。可以越子,没有"中国象棋"的"蹩马腿"限制。
每组数据首先是一个整数N(1<=n<=10),表示要摆N个骑士上去;
接下来是一个8*8的矩阵来描述一个棋盘,’.’表示这个位置是空的,’*’表示这个位置上已经放了皇后了;
数据中的初始棋盘保证是一个合法的八皇后摆法。
1
*.......
....*...
.......*
.....*..
..*.....
......*.
.*......
...*....
2
*.......
....*...
.......*
.....*..
..*.....
......*.
.*......
...*....
1409
题解:
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的更多相关文章
- HDU 4529 郑厂长系列故事——N骑士问题 状压dp
		
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4529 郑厂长系列故事--N骑士问题 Time Limit: 6000/3000 MS (Java/O ...
 - hdu_4529_郑厂长系列故事——N骑士问题(状压DP)
		
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4529 题意:中文,不解释 题解:状压DP,dp[i][j][k][s]表示第i行当前用了j个骑士,i- ...
 - HDU 4539 郑厂长系列故事——排兵布阵 状压dp
		
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事--排兵布阵 Time Limit: 10000/5000 MS (Java/O ...
 - HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...
 - HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
		
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
 - hdu_4539_郑厂长系列故事——排兵布阵(状压DP|最大团)
		
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 题意:中文,不解释 题解:将每一行的状态压缩,然后进行DP,也可以用最大团做.这里我用的DP # ...
 - HDU----(4519)郑厂长系列故事——体检
		
郑厂长系列故事——体检 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
 - HDU 4539郑厂长系列故事――排兵布阵(状压DP)
		
HDU 4539 郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 //#pragma co ...
 - HDU 4539                    郑厂长系列故事——排兵布阵
		
http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Others) ...
 
随机推荐
- CentOS---网络配置具体解释
			
一.配置文件具体解释 在RHEL或者CentOS等Redhat系的Linux系统里.跟网络有关的主要设置文件例如以下: /etc/host.conf 配置域名服务client的控制文件 ...
 - 如何选择Haproxy和Nginx
			
对于做软负载,我们都知道主流的方案有LVS.Haproxy.Nginx!那么对于Haproxy和Nginx,我们如何选择呢?回答这个问题之前,我根据个人使用经验来讲下它们的特点! Haproxy特点 ...
 - BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序
			
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 本章节你将学到: SP中工作流的新功能: 理解工作流管理服务 ...
 - CSS环绕球体的旋转文字-3D效果
			
代码地址如下:http://www.demodashi.com/demo/12482.html 项目文件结构截图 只需要一个html文件既可: 项目截图: 代码实现原理: 该示例的实现过程很简单,主要 ...
 - mysql导出导入所有数据库
			
导出所有数据库 mysqldump -uroot -p123456 --all-databases > /home/aa.sql 导入所有数据库 mysql -uroot -p123456 &l ...
 - leetcode_Multiply Strings
			
描写叙述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
 - spring-web中的WebDataBinder理解
			
Spring可以自动封装Bean,也就是说前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来的是 ...
 - 从英语单词shell想到的
			
shell当初听到以为很高级 后来才知道只是壳而已 百度百科中解释为 shell 在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的c ...
 - 不安装Oracle客户端也能使用PL/SQL
			
解压缩 instantclient_12_1 到 D:\Oracle\instantclient_12_1 在文件夹内建立目录, /NETWORK/ADMIN 在该目录下,新建文件tnsnames.o ...
 - Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
			
using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...