SGU 224.Little Queens

时间限制: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

时间: 2024-08-16 13:49:59

SGU 224.Little Queens的相关文章

SGU 224

题目大意:给定一个n*n的国际象棋棋盘和k个后,问使得所有后互不相攻击方案数. 题解:无脑爆搜(和紫书P193上面那个差不多),我265ms过的,0.75秒就会T,还好,捏一把冷汗. %%%__debug大神今天一下午过了BZOJ1999树网的核加强版,还口口声声地吐槽联赛数据弱,WA3个地方的程序都能A. #include<cstdio> #include<cstdlib> #include<algorithm> #include<iostream> #i

SGU 220~229

SB题一堆,以后为了提高效率,SB题,一眼题一律不码. 220 Little Bishops 题意:求n*n的棋盘上放K个象的放法, 象可以对角线相互攻击 sb题. 221 Big Bishops 如上,一字未变,加个高精度即可,尼玛我现在高精度都能写错,还是滚回pj吧.. 222. Little Rooks 题意:n*n放车,问方案数.n最大10. 尼玛,真的滚回pj组了!?老子还是读过书的. 223 Little King 题意:如上,把车改成王. 状压直接上. 224. Little Qu

Jeff Somers&#39;s N Queens Solutions 最快的n皇后算法

1 /* Jeff Somers 2 * 3 * Copyright (c) 2002 4 * 5 * [email protected] 6 * or 7 * [email protected] 8 * 9 * April, 2002 10 * 11 * Program: nq 12 * 13 * Program to find number of solutions to the N queens problem. 14 * This program assumes a twos compl

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

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

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl

SGU 116 Index of super-prime 数论+完全背包+输出方案

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=116 题意好晦涩 给你一个不超过一万的数 问它最少可以用多少个“超级素数”来表示 使“超级素数”之和等于它 如果无法这样表示 输出0 否则 按非降序形式输出方案 数论部分就模板的问题 没什么说的 完全背包方面也很常规 说说[输出方案] 背包九讲的伪码给的是二维dp[]的方法 实际上稍加改动就可以用在一维数组上 用一个rec[]记录dp[]的当前状态是从哪个状态转移而来(即上一个状态) 通过

SGU 乱搞日志

SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注意自环,连通 1 //sgu101 2 #include<iostream> 3 #include<cstdio> 4 #include <math.h> 5 #include<algorithm> 6 #include<string.h> 7 #i

SGU 275 To xor or not to xor (高斯消元)

题目地址:SGU 275 首先,贪心的思想,每一二进制位上要尽量是1,而能不能是1用高斯消元来解决.当该位有一个可以使之为1的变元时,就说明这位可以为1,而且令该变元控制该位,然后向低位消元. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

SGU 221.Big Bishops(DP)

题意: 给一个n*n(n<=50)的棋盘,放上k个主教(斜走),求能放置的种类总数. Solution : 同SGU 220,加个高精度就好了. code #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> using namespace std; string f[2][250][250], ans;