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) ...
随机推荐
- UTF-8 setup for workspace
In Eclipse, go to Preferences>General>Workspace and select UTF-8 as the Text File Encoding. Th ...
- 【分享】· 图床&在线分享演示文稿
关于图床 什么是图床? 这并不是一个多么高大上的名词概念!用比较通俗的话来说,当你在撰写新文章时,你需要去插入图片以使得你的文章内容更加直观.易懂,这个时候有以下几种办法: 在博客根目录的 sourc ...
- Ubuntu 16.04服务器版查看IP、网关、DNS(非DHCP)
查看IP ifconfig em1 Link encap:Ethernet HWaddr F0:1F:AF:D6:17:DD inet addr:115.238.54.116 Bcast:115.23 ...
- LinuxPAServer19.0.tar.gz压缩包
LinuxPAServer19.0.tar.gz DELPHI XE10.2(TOKYO)开始可以编写LINUX控制台程序.在LINUX上面需要部署LinuxPAServer19.0.tar.gz,即 ...
- NPM安装报错:WARN PACKAGE.JSON, NO REPOSITORY FIELDS
今天在安装npm包时遇到了这个错误,出现如下提示: npm WARN package.json xxx@0.0.0 No repository field. npm WARN package.json ...
- flask调试代码更改、模板更改后立即生效
1.app.DEBUG=True时,代码更改后立即生效 2.APP.jinja_env.auto_reload = True时,模板修改后立即生效,无需重启 参考:https://stackoverf ...
- 利用PPPOE认证获取路由器中宽带账号密码
前言 回家时买了一台极路由准备换掉家里老掉牙的阿里路由器,想进后台看一下宽带账号密码,咦???后台密码是什么来着??? 我陷入了沉思,家里的路由器一般都是pppoe拨号,而路由器在与pppoe认证服务 ...
- DELLR720服务器更换硬盘,启动系统报错:there are offline or missing virtual drivers with preserved cache
linux系统启动过程中给出错误: There are offline or missing virtual drives with preserved cache. Please check the ...
- python之生成excel
#_*_coding:utf-8_*_ import MySQLdb import xlwt from datetime import datetime def get_data(sql): # 创建 ...
- sqoop使用记录
sqoop简介 Sqoop是用来实现结构型数据(如关系数据库)和Hadoop之间进行数据迁移的工具.它充分利用了MapReduce的并行特点以批处理的方式加快数据的传输,同时也借助MapReduce实 ...