题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2136

Problem A

Another n-Queen Problem

I guess the n-queen problem is known by every person who has studied backtracking. In this problem you should count the number of placement of
n queens on an n*n board so that no two queens attack each other. To make the problem a little bit harder (easier?), there are some bad squares where queens cannot be placed. Please keep in mind that bad squares cannot be used to
block queens' attack.

Even if two solutions become the same after some rotations and reflections, they are regarded as different. So there are exactly 92 solutions to the traditional 8-queen problem.

Input

The input consists of at most 10 test cases. Each case contains one integers
n
(3 < n < 15) in the first line. The following n lines represent the board, where empty squares are represented by dots '.', bad squares are represented by asterisks '*'. The last case is followed by a single zero, which should not be
processed.

Output

For each test case, print the case number and the number of solutions.

Sample Input

8
........
........
........
........
........
........
........
........
4
.*..
....
....
....
0

Output for the Sample Input

Case 1: 92
Case 2: 1

Rujia Liu's Present 1: A Tiny Contest of Brute Force



n后问题的加强版,採用普通的回朔会超时。所以用状态压缩和位运算加以优化。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX=20;
int str[MAX];//原串
int n,msk;
char s[MAX];//原串
int dfs(int dep,int dow,int lefd,int rigd){//dow。lefd。rigd表示上一层所能攻击到的区域
if(dep>=n) return 1;
int cur=~( str[dep] | dow | lefd | rigd );//反转得到1表示这一层,能够放的地方,0表示这一层不能够放的地方。
int p=cur&(-cur)&msk;//搞到最后一个非0位
int ret=0;
while(p){
ret+=dfs(dep+1,dow|p,(lefd|p)<<1,(rigd|p)>>1);
cur^=p;//那位置0
p=cur&(-cur)&msk;
}
return ret;
}
int main(){
int cas=0;
while(scanf("%d",&n),n){
msk=(1<<n)-1;
for(int i=0;i<n;i++){
scanf("%s",s);
str[i]=0;
for(int j=0;s[j];j++){
if(s[j]=='*'){
str[i]|=(1<<j);//把不能訪问的区域标志成1
}
}
}
printf("Case %d: %d\n",++cas,dfs(0,0,0,0));
}
return 0;
}





uva 11195 Another queen (用状态压缩解决N后问题)的更多相关文章

  1. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  2. UVA 10160 Servicing Stations(状态压缩+迭代加深)

    [题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...

  3. uva 10817 - Headmaster's Headache ( 状态压缩dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资使得每 ...

  4. hdu 5067 Harry And Dig Machine (状态压缩dp)

    题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...

  5. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  6. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  7. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  8. POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

    题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...

  9. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

随机推荐

  1. Makefile与shell脚本的区别

    引用博客:Makefile与shell脚本区别 在Makefile可以调用shell脚本,但是Makefile和shell脚本是不同的.本文试着归纳一下Makefile和shell脚本的不同. 1.s ...

  2. xmpp 配置数据库 服务器

    一.了解XMPP 协议(标准) XMPP 即时通讯协议 SGIP 短信网关协议 这手机发短信 移动支付和网页支付 0x23232[0,1] 0x23232 0x23232 0x23232 只有协议,必 ...

  3. 使用SeaJS实现模块化JavaScript开发(新)

    本文转自张洋,因为SeaJS更新版本很快,所以原文中很多地方不太适用,在这里发布一个更新版. 前言   SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaSc ...

  4. 【网络流24题】 No.2 太空飞行计划问题 (最大闭合权图 最大流 )

    原题:         W教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合 E={E1,E2,...,Em},和进行这些实验需 ...

  5. 程序异常捕获库 - CrashRpt

    CrashRpt.dll用来在应用程序出现异常crash时,捕获到错误. 并收集出错信息: MiniDump文件.硬件信息.系统信息.出错信息.进程信息.服务信息.驱动信息.启动信息.软件列表.端口信 ...

  6. QT类库与Delphi类库的体系结构对比——两者十分类似!

    今天在看QT对象内存管理的一篇文章时:http://blog.csdn.net/dbzhang800/article/details/6300025想到了一个问题:就是QT类库体系结构与Delphi类 ...

  7. BZOJ_1202_狡猾的商人_(并查集)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1202 n 个月的账单,共 m 组数据,每一组数据包括 x , y , t ,表示从 x 月到 ...

  8. [DP] LCS小结

    额..失误.. LCS是Longest Common Subsequence的缩写,即最长公共子序列.一个序列,如果是两个或多个已知序列的子序列,且是所有子序列中最长的,则为最长公共子序列. DP.O ...

  9. WinPcap编程入门实践

    转自:http://www.cnblogs.com/blacksword/archive/2012/03/19/2406098.html WinPcap可能对大多数人都很陌生,我在这里就先简单介绍一下 ...

  10. ASP.NET使用EasyUI-DataGrid + ashx + JQuery Ajax:实现数据的增删查改,查询和分页!

    转自:http://www.cnblogs.com/lt-style/p/3457399.html 数据表: 学生表:学生编号.姓名.性别.班级编号.年龄 班级表:班级编号.班级名称 开发过程: 1. ...