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) ...
随机推荐
- 出自 HTML4 规范的可用颜色字符串值列表(常用颜色名称及对应的十六进制值)
据称由于 HTML5 没有修改专属的颜色,HTML4 的颜色都可以在 HTML5 中正确显示. 出自 HTML4 规范的可用颜色字符串值列表如下,此表来源是 http://www.lovean.com ...
- 通过apache的mod_status 统计占资源的脚本
apache的mod_status模块,提供了对apache运行时的一些统计信息,对apache的管理员来说很有意义. 一.加载apache的mod_status模块 各种系统下,加载apache模块 ...
- KVO(1)
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong)UIButt ...
- 如何删除mysql 主键索引
如果一个主键是自增长的,不能直接删除该列的主键索引, 应当先取消自增长,再删除主键特性 alter table 表名 drop primary key; [如果这个主键是自增的,先取消自增长.] ...
- Shell脚本之:for
与其他编程语言类似,Shell支持for循环. for循环一般格式为: for 变量 in 列表 do command1 command2 ... commandN done 列表是一组值(数字.字符 ...
- TCP/IP协议(数据封装与拆装过程)
IP地址 = 网络地址+主机地址 = 网络地址 + 子网地址 + 主机地址 应用进程之间的通信被称之为端到端的通信. 传输层与网络层之间的区别:传输层为应用进程间提供了端到端的逻辑通信:网络层提 ...
- cmake学习之- cmake_parse_arguments
最后更新: 2019-06-08 一.指令介绍 cmake_parse_arguments 为解析函数(function)或 宏(macros) 参数的命令: cmake_parse_argument ...
- andorid中发送短信页面以及邮件发送
跳转到发送短信页面 Uri smsToUri = Uri.parse("smsto://10086"); Intent mIntent = new Intent( android. ...
- POJ3009 Curling 2.0(DFS)
迷宫问题求最短路. 略有不同的是假设不碰到石头的话会沿着一个方向一直前进,出界就算输了.碰到石头,前方石头会消失,冰壶停在原地. 把这个当作状态的转移. DFS能够求出其最小操作数. #include ...
- 常用string函数分析
string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...