题目链接: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. Ubuntu下Memcache的安装与基本使用

    安装Memcache Memcache分为两部分,Memcache服务端和客户端.Memcache服务端是作为服务来运行的,所有数据缓存的建立,存储,删除实际上都是在这里完成的.客户端,在这里我们指的 ...

  2. Nopcommerce 3.7 增加了Redis 作为缓存啦

    Nopcommerce 3.7  版增加了Redis 缓存,相比之前内存缓存, 感觉使用了Redis 作为缓存,明显加快了Web页面响应速度! 废话少说 拉代码: 1 git clone https: ...

  3. VS2010/MFC编程入门之十四(对话框:向导对话框的创建及显示)

    原文地址:http://www.jizhuomi.com/software/166.html 上一讲鸡啄米讲了属性页对话框和相关的两个类CPropertyPage类和CPropertySheet类,对 ...

  4. adb logcat 查看日志

    使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] .. ...

  5. 一个奇怪的编码 big5-hkscs

    # --*-- coding:utf-8 --*-- import urllib2 import urllib postDict = { 'IsExist_Slt_Part_Id': 'False', ...

  6. Android判断界面

         仿造微信,第一次进入去引导界面,否则进启动界面. package edu.hpu.init; import edu.hpu.logic.R; import android.app.Activ ...

  7. C#中数据库连接的配置文件

    在C#2010中,如何保存和访问数据库的连接字符串呢? 在Winform下要新增App.config文件,在Asp.net下要新增web.config文件. 1.打开配置文件添加相关代码后如下即可: ...

  8. 测来测去,感觉REQUESTS最实在

    URLLIB,URLLIB2,PYCURL,HTTPIE,,,在测试PUT及认证时,还是REQUESTS胜出.. 测试过程及样例代码如下: import urllib import urllib2 i ...

  9. duilib入门简明教程 -- VS环境配置(2) Alberl

      既然是入门教程,那当然得基础点,因为搜索duilib相关资料时,发现有些小伙伴到处都是编译错误,以及路径配置错误等等,还有人不知道SVN,然后一个个文件手动下载的.     其实吧,duilib的 ...

  10. SectionIndexer中的getSectionForPosition()与getPositionForSection()

    大家在做字母索引的时候常常会用到SectionIndexer这个类,里面有2个重要的方法 1.   getSectionForPosition()通过该项的位置,获得所在分类组的索引号 2. getP ...