时间限制:0.75s

空间限制:6M

题意

n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数。


Solution:

状态压缩+位运算  搜索。

首先我们从上往下逐行放置,

DFS(line, row, l, r, k)

line :当前行号

row:列状态

l:\ 左上对角线状态

r:/右上对角线状态

k:已放置棋子数

对于每一行有不放或者放一个棋子两种方案

放一个棋子时又要考虑哪些位置可以放置,

状态压缩(row,r,l):

例如当n=4时

二进制数

1=(0001)2代表在第一个位置放置了棋子

同理(1111) 代表已经放满;

可放状态(pos):

15=(1111)代表全部位置可放

1=(0001) 代表右边第一个位置可放

0=(0000)代表无法再放

对(pos=~(l | row | r))   pos得到当前行所有可放置的位置(可以自己模拟一下)

数状数组中出现的 p= pos& - pos 得到最后一个1 的位置,即一个可放位置。

状态(row,l,r  )|  p 时,即更新当前行放置后的三个状态

例如  p=1(0001),当前放右一;

row=8(1000),左一已不可放。

row=p | row=9(1001),  即右一和左一都不可以再放

当line+1,即搜索下一行时,l和r变化

以左上对角线状态l为例

初始为(0000)

当右2放置1个棋子时,当前行(0010)

由于是左上对角线,下一行l变为(0001),即 l>>1;

r同理,即r<<1.

row列状态在行变化的时候不需要更新

代码

60ms+2KB  Accepted

#include <cstdio>
int n, sum, max, k, m;
void dfs (int line , int row, int l, int r, int k) {
int pos, p, i;
if (line > n){
if(k == m) sum++;
return;
}
dfs (line + 1, row, l>>1, r<<1, k);
if (row != max) {
pos = max & (~ (row | l | r) );
while (pos != 0) {
p = pos & -pos;
pos = pos - p;
dfs (line+1,row | p, (l | p) >> 1, (r | p) << 1, k + 1);
}
}
}
int main() {
scanf ("%d %d", &n, &m);
max = (1 << n) - 1;
dfs (1, 0, 0, 0, 0);
printf ("%d", sum);
}

  

  

SGU 224.Little Queens的更多相关文章

  1. 今日SGU 5.26

    #include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl ...

  2. Android Weekly Notes Issue #224

    Android Weekly Issue #224 September 25th, 2016 Android Weekly Issue #224 本期内容包括: Google Play的pre-lau ...

  3. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  4. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  5. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  6. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  7. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

  8. Jeff Somers's N Queens Solutions 最快的n皇后算法

    /* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...

  9. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

随机推荐

  1. android信号强度

    android定义了2种信号单位:dBm(1毫瓦的分贝数)和asu(alone signal unit 独立信号单元). 它们之间的关系是:dBm =-113+2*asu,这是google给andro ...

  2. Codevs 3304 水果姐逛水果街Ⅰ 线段树

    题目: http://codevs.cn/problem/3304/   时间限制: 2 s   空间限制: 256000 KB   题目等级 : 钻石 Diamond 题解       题目描述 D ...

  3. asp json

    <script language="JScript" runat="Server">function toObject(json) {    eva ...

  4. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. Android View 事件分发机制详解

    想必很多android开发者都遇到过手势冲突的情况,我们一般都是通过内部拦截和外部拦截法解决此类问题.要想搞明白原理就必须了解View的分发机制.在此之前我们先来了解一下以下三个非常重要的方法: di ...

  6. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  7. 1242Rescue (优先队列BFS)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  8. Mysql性能优化那些事

    ​    ​对于全栈而言,数据库技能不可或缺,关系型数据库或者nosql,内存型数据库或者偏磁盘存储的数据库,对象存储的数据库或者图数据库--林林总总,但是第一必备技能还应该是MySQL.从LAMP的 ...

  9. 保证相同类型的MDI子窗体只会被打开一次的方法

    本文转载:http://www.cnblogs.com/Ricky81317/archive/2008/09/17/1292443.html 看到论坛中有朋友问,如何可以保证在MDI主窗体中,同一类型 ...

  10. HDU 4901(杭电多校训练#3 1005题)The Romantic Hero(DP)

    题目地址:HDU 4901 这题没想到最后竟然可以做出来.. .. 这题用了两次DP,先从前往后求一次异或的.再从后往前求一次与运算的. 各自是 1:求异或的时候,定义二维数组huo[1000][10 ...