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) ...
随机推荐
- C#反射获取属性值和设置属性值
/// /// 获取类中的属性值 /// public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj ...
- 工厂方法模式之C++实现
说明:本文仅供学习交流,转载请标明出处.欢迎转载. 工厂方法模式与简单工厂模式的差别在于:在简单工厂模式中.全部的产品都是有一个工厂创造,这样使得工厂承担了太大的造产品的压力,工厂内部必须考虑所以的产 ...
- ViewPager+Fragment 滑动菜单效果 实现步骤
1.xml中引用ViewPager <android.support.v4.view.ViewPager android:id="@+id/viewPa ...
- UIView属性的动画
//标记着动画块的开始,第一个参数表示动画的名字,起到标识作用 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDurat ...
- .NET实现爬虫
前几天看到一个.NET Core写成的爬虫,有些莫名的小兴奋,之前一直用集搜客去爬拉勾网的招聘信息,这个傻瓜化工具相当于用HTML模板页去标记DOM节点,然后在浏览器窗口上模拟人的浏览行为同时跟踪节点 ...
- select设置innerHMTL
select控件在标准浏览器下可以直接innerHTML设置内容,IE则不行. HTML结构: <form name="form1"> <select name= ...
- Linux下Nginx安全证书ssl配置方法
分享下我是如何一步步在Nginx上配置SSL的.首先,确保安装了OpenSSL库,并且安装Nginx时使用了–with-http_ssl_module参数. 初学者或者菜鸟建议使用LNMP进行一键安装 ...
- CSS - 修改input - placeholder 和 readonly 的样式
placeholder ::-webkit-input-placeholder { /* WebKit browsers */ color: #999999; } :-moz-placeholder ...
- php计算两个经纬度地点之间的距离(转)
php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...
- 兔子--改动Android Studio的快捷键,改动成eclipse的快捷键
仅仅须要2步 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill ...