【BZOJ1087 || SCOI2005】互不侵犯King
来源:互联网 发布:ds cloud windows 编辑:程序博客网 时间:2024/05/01 10:59
【题目描述】
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。
1 <=N <=9, 0 <= K <= N * N
【简要分析】
水题,大神请【Ctrl + W】
裸搜……咳咳,显然过不了
暴力dp也是可以过的
也容易想到用状态压缩去优化转移
然后最后再打个表,恭喜你,BZOJrank1非你莫属
有几个实现技巧
1. 把每个二级制数有多少个1用dp预处理出来(也可以用matrix67讲稿内的方法)
2. 把一行内国王的各种状态预处理出来
3. 利用位运算判断状态是否合法
【Code】
http://ideone.com/XAjBgR
#include <algorithm>#include <ctime>#include <cmath>#include <set>#include <map>#include <stack>#include <vector>#include <string>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <iostream> using namespace std; #define sci stack <int>#define vci vector <int>#define vcs vector <string>#define vcd vector <double>#define vci64 vector <long long> const int maxn = 9 + 5;const int maxk = 9 * 9 + 5; typedef unsigned int uint;typedef long long int64;typedef unsigned long long uint64; template <class T> inline T Sqr(const T & x) { return x * x; }template <class T> inline T Abs(const T & x) { return x > 0 ? x : -x; }template <class T> inline T Min(const T & a, const T & b) { return a < b ? a : b; }template <class T> inline T Max(const T & a, const T & b) { return a > b ? a : b; }template <class T> inline T Ksm(const T & a, const T & b, const T & m) { T _ = 1; for (; b; b >>= 1, a = a * a % m) (b & 1) ? _ = _ * a % m : 0; return _ % m; }template <class T> inline void Swap(T & a, T & b) { T _; _ = a; a = b; b = _; } int n, k, cn[1 << 9 + 1], cnt, state[1 << 9 + 1];int64 f[maxn][maxk][1 << 9 + 1], ans; void dfs(int wid, int sta){ if (wid == n) return state[++cnt] = sta, (void) 0; if (dfs(wid + 1, sta << 1), !(sta & 1)) dfs(wid + 1, sta << 1 | 1);} bool check(int a, int b) { return !(a & b) && !((a << 1) & b) && !((a >> 1) & b); } int main(){ scanf("%d%d", &n, &k), dfs(0, 0); for (int i = 1; i <= (1 << n) - 1; ++i) cn[i] = cn[i / 2] + (i & 1); f[0][0][1] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j <= k; ++j) for (int li = 1; li <= cnt; ++li) for (int ru = 1; ru <= cnt; ++ru) if ((j >= cn[state[ru]]) && (check(state[li], state[ru]))) f[i][j][ru] += f[i - 1][j - cn[state[ru]]][li]; for (int i = 1; i <= cnt; ++i) ans += f[n][k][i]; printf("%lld", ans); return 0;}
- 【BZOJ1087 || SCOI2005】互不侵犯King
- [BZOJ1087][SCOI2005]互不侵犯King
- 【bzoj1087】[SCOI2005]互不侵犯King
- 【SCOI2005】【BZOJ1087】互不侵犯King
- [BZOJ1087] [SCOI2005]互不侵犯King
- bzoj1087【SCOI2005】互不侵犯King
- bzoj1087: [SCOI2005]互不侵犯King
- bzoj1087: [SCOI2005]互不侵犯King
- bzoj1087[SCOI2005]互不侵犯King
- 【bzoj1087】[SCOI2005]互不侵犯King
- [BZOJ1087][SCOI2005]互不侵犯king
- bzoj1087: [SCOI2005]互不侵犯King
- bzoj1087: [SCOI2005]互不侵犯King
- BZOJ1087: [SCOI2005]互不侵犯King
- bzoj1087[SCOI2005]互不侵犯King
- [BZOJ1087][SCOI2005]互不侵犯King
- [BZOJ1087][SCOI2005]互不侵犯King
- bzoj1087 [SCOI2005]互不侵犯King
- 学习存储技术的5个阶段
- C++之友元
- 使用nginx配置android文档本地站点
- myeclipse8.6 使用时注册问题破解之法
- 原子操作
- 【BZOJ1087 || SCOI2005】互不侵犯King
- Solving 11 Likely Problems In Your Multithreaded Code
- 有关CPU DSP MCU MPU的几个概念
- LinuxC/C++编程基础(21) 使用boost::asio搭建服务器简单实例(续)
- 正则表达式 grep 的使用
- applicationContext.xml 配置文件的存放位置
- 绚丽的ListView表格效果的实现
- Vim的一些基本配置(比较全面些,可以少找其他资料)
- 数组与容器