zoj2479 Cover the Rectangular Ground
肯定是dfs搜一下的,但是呢存在一个很大的剪枝,也就是面积必定要是相等的,那么如何去操作呢,可以想到的是二进制枚举选取的方法,然后把方法中选取的矩形面积求和并判断一下即可,然后dfs搜索,要注意的是,需要维持现状的变量在变化之前一定要记录,要变回来时,一定要变回来,另外在dfs中还可以判断覆盖是否合理,以及是否已经没有可找到的空白区域。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int T,n,m,q,x[],y[],p[],sx,sy,b;
int a[][];
bool f[],ans;
bool overlap(int i,int v,int t){
int ex = x[i],ey = y[i];
if(v) swap(ex,ey);
if(sx + ex - >= n || sy + ey - >= m) return false;
for(int j = sx;j < sx + ex;++j){
for(int k = sy;k < sy + ey;++k){
if(t && a[j][k]) return false;
}
}
for(int j = sx;j < sx + ex;++j){
for(int k = sy;k < sy + ey;++k){
a[j][k] = t;
}
}
return true;
}
bool findempty(){
for(int i = sx;i < n;++i){
for(int j = ;j < m;++j){
if(!a[i][j]){
sx = i,sy = j;
return true;
}
}
}
return false;
}
bool check(){
for(int i = ;i < n;++i){
for(int j = ;j < m;++j){
if(!a[i][j]) return false;
}
}
return true;
}
void dfs(){
bool flag = false;
for(int i = ;i < q;++i){
if((b & p[i]) && !f[i]){
flag = true;
f[i] = true;
int tx = sx,ty = sy;
if(overlap(i,,)){
if(findempty()) dfs();
else{
ans = true;
return;
}
if(ans) return;
sx = tx,sy = ty;
overlap(i,,);
}
if(!overlap(i,,)){
f[i] = false;
continue;
}
if(findempty()) dfs();
else{
ans = true;
return;
}
if(ans) return;
sx = tx,sy = ty;
overlap(i,,);
f[i] = false;
}
}
if(!flag && check()) ans = true;
}
bool solve(){
for(int i = ;i < p[q];++i){//枚举选取哪一些
int s = ;
for(int j = ;j < q;++j){
if(i & p[j]) s = s + x[j] * y[j];
}
if(s != n * m) continue;
b = i;
ans = false;
sx = sy = ;
dfs();
if(ans) return true;
}
return false;
}
int main(){
/*submit failed*/
/*submit failed*/
/*submit failed*//*submit failed*//*submit failed*/
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
cin >> T;
p[] = ;
for(int i = ;i <= ;++i) p[i] = p[i - ] << ;
while(T--){
scanf("%d%d",&n,&m);
cin >> q;
for(int i = ;i < q;++i){
scanf("%d%d",&x[i],&y[i]);
}
memset(a,,sizeof(a));
memset(f,false,sizeof(f));
if(solve()) puts("YES");
else puts("NO");
}
return ;
}
zoj2479 Cover the Rectangular Ground的更多相关文章
- Entity Framework 6 Recipes 2nd Edition(11-12)译 -> 定义内置函数
11-12. 定义内置函数 问题 想要定义一个在eSQL 和LINQ 查询里使用的内置函数. 解决方案 我们要在数据库中使用IsNull 函数,但是EF没有为eSQL 或LINQ发布这个函数. 假设我 ...
- HBase 数据模型(Data Model)
HBase Data Model--HBase 数据模型(翻译) 在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的 ...
- 城市边界线预测(根据灯光指数)(PUL)
1.EXEALL.m function EXEALL(FilePath, FileName)%执行所有流程% FilePath: 文件夹所在路径% FileName: 文件夹名称 FullPath = ...
- poj 1266 Cover an Arc.
http://poj.org/problem?id=1266 Cover an Arc. Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- UVALive 3959 Rectangular Polygons (排序贪心)
Rectangular Polygons 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/G Description In thi ...
- poj 2836 Rectangular Covering
Rectangular Covering Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2776 Accepted: 7 ...
- Dancing Links and Exact Cover
1. Exact Cover Problem DLX是用来解决精确覆盖问题行之有效的算法. 在讲解DLX之前,我们先了解一下什么是精确覆盖问题(Exact Cover Problem)? 1.1 Po ...
- img及父元素(容器)实现类似css3中的background-size:contain / background-size:cover
img及父元素(容器)实现类似css3中的background-size:contain / background-size:cover <!DOCTYPE html> <html ...
- 集合覆盖 顶点覆盖: set cover和vertex cover
这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...
随机推荐
- 【Hibernate】多对多关系的表达
User.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate ...
- javascript实现弹层效果
首先,需要有一个按钮来模拟登录: <button id="btnLogin"class="login-btn">登录</button> ...
- CAS server 连接mysql的deployerConfigContext.xml配置
1.deployerConfigContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <b ...
- Mysql——Innodb和Myisam概念与数据恢复
Innodb和Myisam是Mysql常见的两种数据存储引擎.没有研究过Oracle.SQL Server等数据库,所以下面只针对Mysql. 一.两种方式的数据存储结构: 在Myisam下,数据库的 ...
- 关于AMAZON SES设置的一些要点
1.首先要有一个企业邮箱,如果没有可以去腾讯(http://exmail.qq.com/onlinesell/intro)申请一个,网易也有,不过解析几次搞了两天都是未通过,腾讯几分钟就好了 企业邮箱 ...
- C/C++ 各进制赋值、int/char转换、sscanf/sprintf、位操作运算
一.各进制赋值 1.十六进制赋值 int i=0x12AD; int i=0X12AD; int i=0x12Ad; int i=0X12Ad; //以上都是十六进制,表示十进制173: 2.八进制赋 ...
- python与shell通过微信企业号发送消息
python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...
- nutz配置druid监控
druid 提供了一个web端的监控页面, 搭建起来不算麻烦, 建议添加. 打开web.xml, 在nutz的filter之前, 加入Web监控的配置 <filter> <filte ...
- Winsock2_WSADATA
使用Winsock2进行win下网络编程的第一步是初始化Winsock.其中需要创建一个WSADATA类型的变量,这个变量用来保存Windows socket的实现信息. typedef struct ...
- windows测试物理网络
ping 192.168.10.88 -t ,参数-t是等待用户去中断测试