NYOJ 492 King (状态压缩)
做题感悟:做完这题发现状态压缩有很多须要优化的地方。
解题思路:状态压缩
開始自己用的一般的思路,就和炮兵阵地,郑厂长等题类似的方法做的,開始超时,然后把数组开到了最小的极限就险过。然后看了别人的代码感觉须要优化(注意)的地方太多了。
首先我们这题能够预处理出来上下两行相应的合法状态,这样我们就确定下来上下两行相应的状态了。这是第一步的优化 。由于当前行仅仅与上一行有关(这里仅仅考虑当前行对上一行的影响就能够)。我们能够把 dp 第一维开成 2 的就能够了,这是第二步的优化,尽管这步作用有点小。最后
,我们还能够抛弃一些不合法的输入 ,假如是 n * n 的棋盘,一行最多能够防止(n+1)/ 2 个,然后 n 行最多能够放置 ( n + 1 ) / 2 * ( n + 1 ) / 2 个。 为什么是 ( n + 1 ) / 2 行放置呢 ? 由于每一个格子攻击相邻的八个格子。不要用n / 2 ,由于假设 奇数行的话就少算了一行。
这题也能够用DFS递推和铺方格差点儿相同。这样也相当于预处理出上下两行相应的状态。
代码:
#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT long long int
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1e9 + 7 ;
const int MY = 1400 + 5 ;
const int MX = 150 ;
int n ,m ,num ,top ;
INT dp[2][MX][101] ;
int key[MX] ,h[MX] ;
struct node
{
int x ,y ,c ;
}P[MY] ;
void init() // 预处理合法状态
{
for(int S = 0 ;S < (1<<n) ; ++S)
if(!(S&(S>>1)) && !(S&(S<<1)))
{
int nx = 0 ;
for(int i = 0 ;i < n ; ++i)
if(S&(1<<i))
nx++ ;
key[num] = S ;
h[num++] = nx ;
}
}
int main()
{
while(~scanf("%d%d" ,&n ,&m))
{
if(((n+1)/2)*((n+1)/2) < m) // 抛弃不合法的状态
{
puts("0") ;
continue ;
}
num = 0 ; top = 0 ;
init() ; // 预处理合法状态
for(int i = 0 ;i < num ; ++i) // 预处理上下两行相应的合法状态
for(int j = 0 ;j < num ; ++j)
if(!(key[i]&(key[j]>>1)) && !(key[i]&(key[j]<<1)) && !(key[i]&key[j]))
{
P[top].x = i ;
P[top].y = j ;
P[top++].c = h[j] ;
}
memset(dp[0] ,0 ,sizeof(dp[0])) ;
dp[0][0][0] = 1 ;
for(int i = 1 ;i <= n ; ++i)
{
memset(dp[i&1] ,0 ,sizeof(dp[i&1])) ;
for(int j = 0 ;j < top ; ++j) // 上一行的合法状态
for(int t = 0 ;t <= m ; ++t) // 安放的个数
if(t + P[j].c <= m) // 个数限制
dp[i&1][P[j].y][t+P[j].c] += dp[(i+1)&1][P[j].x][t] ;
}
INT ans = 0 ;
for(int i = 0 ;i < num ; ++i)
ans += dp[n&1][i][m] ;
printf("%lld\n" ,ans) ;
}
return 0 ;
}
NYOJ 492 King (状态压缩)的更多相关文章
- bzoj 1087 [SCOI2005]互不侵犯King 状态压缩dp
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Descripti ...
- BZOJ 1087 互不侵犯King 状态压缩DP
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1087 题目大意; 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国 ...
- BZOJ1087 [SCOI2005]互不侵犯King 状态压缩动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1087 题意概括 在n*n的棋盘上面放k个国王,使得他们互相无法攻击,问有多少种摆法. 题解 dp[ ...
- 【bzoj1087】互不侵犯King 状态压缩dp
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1087 [题解] 用f[i][j][k]表示前i行放了j个棋子且第i行的状态为k的方案数. ...
- HDU 3681 Prison Break(BFS+二分+状态压缩DP)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- 状态压缩动态规划 状压DP
总述 状态压缩动态规划,就是我们俗称的状压DP,是利用计算机二进制的性质来描述状态的一种DP方式 很多棋盘问题都运用到了状压,同时,状压也很经常和BFS及DP连用,例题里会给出介绍 有了状态,DP就比 ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
随机推荐
- 「JXOI2018」游戏
注意输出的应该是 所有方案的和,,而不是期望. 我们不妨把依赖关系建图,可以发现 所有没有入度的点都被查水表了一次 是 游戏结束的 充要条件. 于是我们只需要知道有多少没有入度的点,然后再排列算一算就 ...
- [LOJ6281]数列分块入门 5
题目大意: 给你一个长度为$n(n\leq50000)$的序列$A(0\leq A_i<2^{31})$,支持进行以下两种操作: 1.将区间$[l,r]$中所有数开方: 2.询问区间$[l,r] ...
- 第一讲work(axe)
1,Dao package com.songyan.Dao; public interface Axe { public void chop(); } package com.songyan.Dao; ...
- exports 与 module.exports 的区别
exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用. 那么require()一个模块时,到底做 ...
- Visio整体移动
同理选中第一列的两个,按向右箭头,整体友谊.
- FreeRTOS+FreeModbus+神舟IV号
下面的这个例子是FreeModbus和FreeRTOS在神舟IV号上的应用,仅当做学习用途. 这个demo完成的功能也比较简单,创建了两个任务,一个任务用于控制板子上的LED1,使它每1秒钟闪烁一次. ...
- JAVA实现网页快照,存为图片格式
原文:http://blog.csdn.net/java2000_net/article/details/3643528 截取的google的效果,将就吧,不是特别好. 但是作为普通的应用,我想这个效 ...
- APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate notice, with the fields e ...
- ulimit 不生效
ulimit is a shell builtin like cd, not a separate program. sudo looks for a binary to run, but there ...
- andriod绘制图形
使用view画图,有两个重要的组件需要介绍: (1)Paint 可以理解为画刷或者画笔,去主要用来设置绘图使用的颜色.填充方式.透明度.字体以及字体样式等. (2)Canvas 画布,在view上显示 ...