uva 11195 Another queen (用状态压缩解决N后问题)
题目链接: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后问题)的更多相关文章
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
- UVA 10160 Servicing Stations(状态压缩+迭代加深)
[题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资使得每 ...
- hdu 5067 Harry And Dig Machine (状态压缩dp)
题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...
- 状态压缩DP(大佬写的很好,转来看)
奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- 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 ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
随机推荐
- Excel Skill (1) -- 判断时如何去掉框里的空格
使用命令 TRIM 说明: Purpose. Remove extra spaces from text. Text with extra spaces removed. =TRIM (text) t ...
- 解决android模拟器太大,小屏幕无法完全显示的问题
http://hi.baidu.com/conanx/blog/item/05479befd6534d03fdfa3cbb.html 安装上Android模拟器之后,开启一个Android 2.2的模 ...
- Android手机上监听短信的两种方式
Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...
- uva 1453 - Squares
旋转卡壳算法: 直接在这个上面粘的模板 主要用途:用于求凸包的直径.宽度,两个不相交凸包间的最大距离和最小距离··· 这题就是求凸包的直径 #include <cstdio> #inclu ...
- iOS,object-c传参c语言的二维数组
有那么一瞬间,懒得用NSArray,NSNumber,NSValue等一大堆蛋疼的转换,所以就定义了一个C的二维数组,反正OC支持C混编,可是蛋疼往往是传递的,这里不疼了,哪里就要疼,想把一个c的二维 ...
- 两种方法,获取磁盘剩余空间--PYTHON
import ctypes import os import platform import sys def get_free_space_mb(folder): """ ...
- 【网络流24题】No. 13 星际转移问题 (网络判定 最大流)
[题意] 由于人类对自然资源的消耗, 人们意识到大约在 2300 年之后, 地球就不能再居住了.于是在月球上建立了新的绿地,以便在需要时移民. 令人意想不到的是, 2177 年冬由于未知的原因, 地球 ...
- LREM key count value
LREM key count value Available since 1.0.0. Time complexity: O(N) where N is the length of the list. ...
- 机器学习10大经典算法.doc
详见 F:\工程硕士\d电子书\26 数据挖掘 小结: 1. C4.5 C4.5算法是机器学习算法中的一种分类决策树算法,其核心算法是ID3算法. C4.5算法继承了ID3算法的优点,并在以下几方面 ...
- Cookies和Session理论总结
今天主要学习了Cookies和Session,网络上关于这方面的知识可谓很多,让人眼花缭乱,在此作一个小结.本文不讲多,不讲什么高大上的,只是抛出一块砖,讲三个问题:①什么是Cookies和Sessi ...