给出一个n\times m的01矩阵,以及\(h,w\),表示一次可以把矩阵的一个\(h\times w\)的小矩阵变为全0,问至少要多少次可以把整个矩阵变为全0。\(n,m\le 15\)。

分析

注意到\(n,m\)非常小,我们可以直接暴力搜索。每次都可以把\(h\times w\)的小矩阵变为全0,那么贪心地想,同一个小矩阵肯定不会消两次。所以我们把每个原来为1的格子看成列,每一种覆盖看成行,那么这就是一个重复覆盖的问题。

重复覆盖问题一定要记得加剪枝

代码

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cstring>
using namespace std;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=17;
const int maxm=maxn*maxn;
const int maxp=maxm*maxm;
const int inf=1e9+7;
bool a[maxn][maxn];
int ids[maxn][maxn];
int ans,n,m;
int id(int x,int y) {
return (x-1)*m+y;
}
struct node {
int l,r,u,d,row,col;
};
struct DLX {
node p[maxp];
int tot,last[maxm],size[maxm];
bool tic[maxm];
void clear(int n) {
memset(p,0,sizeof p),memset(last,0,sizeof last),memset(size,0,sizeof size),tot=n;
p[0]=(node){n,1,0,0,0,0};
for (int i=1;i<=n;++i) p[i]=(node){i-1,i+1,i,i,0,i},last[i]=i;
p[n].r=0;
}
void build(int row,int a[],int len) {
if (!len) return;
p[++tot]=(node){tot,tot,last[a[1]],p[last[a[1]]].d,row,a[1]};
p[p[tot].d].u=p[p[tot].u].d=last[a[1]]=tot;
++size[p[tot].col];
for (int i=2;i<=len;++i) {
int x=a[i];
p[++tot]=(node){tot-1,p[tot-1].r,last[x],p[last[x]].d,row,x};
p[p[tot].d].u=p[p[tot].u].d=p[p[tot].l].r=p[p[tot].r].l=last[x]=tot;
++size[p[tot].col];
}
}
void del(int c) {
for (int i=p[c].d;i!=c;i=p[i].d) p[p[i].l].r=p[i].r,p[p[i].r].l=p[i].l,--size[p[i].col];
}
void back(int c) {
for (int i=p[c].u;i!=c;i=p[i].u) p[p[i].l].r=p[p[i].r].l=i,++size[p[i].col];
}
int est() {
memset(tic,0,sizeof tic);
int ret=0;
for (int i=p[0].r;i;i=p[i].r) if (!tic[i]) {
++ret;
tic[i]=true;
for (int j=p[i].d;j!=i;j=p[j].d) for (int k=p[j].r;k!=j;k=p[k].r) tic[p[k].col]=true;
}
return ret;
}
void dance(int now) {
if (!p[0].r) {
ans=min(ans,now);
return;
}
if (now+est()>=ans) return;
int first=p[0].r;
for (int i=p[0].r;i;i=p[i].r) if (size[i]<size[first]) first=i;
if (p[first].d==first) return;
for (int i=p[first].d;i!=first;i=p[i].d) {
del(i);
for (int j=p[i].r;j!=i;j=p[j].r) del(j);
dance(now+1);
for (int j=p[i].l;j!=i;j=p[j].l) back(j);
back(i);
}
}
} dlx;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
while (~scanf("%d%d",&n,&m)) {
ans=inf;
int ts=0;
for (int i=1;i<=n;++i) for (int j=1;j<=m;++j) {
a[i][j]=read();
if (a[i][j]) ids[i][j]=++ts;
}
dlx.clear(ts);
int tn=read(),tm=read();
for (int i=1;i<=n-tn+1;++i) for (int j=1;j<=m-tm+1;++j) {
static int c[maxm];
int tot=0;
for (int x=i;x<i+tn;++x) for (int y=j;y<j+tm;++y) if (a[x][y]) c[++tot]=ids[x][y];
dlx.build(id(i,j),c,tot);
}
dlx.dance(0);
printf("%d\n",ans);
}
return 0;
}

fzu1686-神龙的难题的更多相关文章

  1. FZU1686 神龙的难题 —— Dancing Links 可重复覆盖

    题目链接:https://vjudge.net/problem/FZU-1686 Problem 1686 神龙的难题 Accept: 812    Submit: 2394 Time Limit: ...

  2. FZU1686 神龙的难题 dancing links 重复覆盖

    分析:每次可以打一个小矩阵的怪,然后把每个怪看成一列,然后每个小矩阵看成一行,枚举左上角就行 注:然后注意总共的节点数是新图的行*列的个数,不是原图 #include<cstdio> #i ...

  3. FZU 1686 神龙的难题 (重复覆盖)

    Problem 1686 神龙的难题 Accept: 397    Submit: 1258Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  4. FZU 1686 神龙的难题 (DLX)

    神龙的难题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  5. FZU 1686 神龙的难题(DLX反复覆盖)

    FZU 1686 神龙的难题 pid=1686" target="_blank" style="">题目链接 题意:中文题 思路:每个1看成列, ...

  6. FZU 1686 神龙的难题 DLX反复覆盖

    DLX反复覆盖: 须要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462    Submit: 1401 Time Limit: 1000 mSec    Memory L ...

  7. [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)

    Problem 1686 神龙的难题 Accept: 444    Submit: 1365 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Pro ...

  8. (简单) FZU 1686 神龙的难题 , DLX+可重复覆盖。

    Description 这是个剑与魔法的世界.英雄和魔物同在,动荡和安定并存.但总的来说,库尔特王国是个安宁的国家,人民安居乐业,魔物也比较少.但是.总有一些魔物不时会进入城市附近,干扰人民的生活.就 ...

  9. FZU Problem 1686 神龙的难题 重复覆盖

    题目链接 给出大矩形的长宽, 矩形里面有1,0两个值, 给出小矩形的长宽, 求用最少的小矩形覆盖所有的1. 重复覆盖的模板题. #include <iostream> #include & ...

  10. 舞蹈链(DLX)

    舞蹈链(DLX) Tags:搜索 作业部落 评论地址 一.概述 特别特别感谢这位童鞋His blog 舞蹈链是一种优美的搜索,就像下面这样跳舞- 舞蹈链用于解决精确覆盖或者重复覆盖的问题 你可以想象成 ...

随机推荐

  1. vue跨域访问

    第一次创建vue项目,画完静态页面一切顺利,准备和后台进行联调,问题来了,无论怎么调试使用Axios,jQuary还是使用原生的Ajax请求都访问不通(前提条件,另外一个人的电脑当成服务器,进行访问) ...

  2. 「LeetCode」0001-Two Sum(Ruby)

    题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...

  3. JS获取HTML DOM元素的8种方法

    什么是HTML DOM 文档对象模型(Document Object Model),是W3C组织推荐的处理可扩展置标语言的标准编程接口.简单理解就是HTML DOM 是关于如何获取.修改.添加或删除 ...

  4. javac 编译过程

    javac 编译过程     一.解析与填充符号表: 1.  语法.此法分析:          a) 语法分析:将源代码字符流转换为标记(Token:编译过程最小元素)集合.          b) ...

  5. Spring Boot 示例项目

    Spring Boot 基于注解式开发 maven REST 示例项目    项目地址:https://github.com/windwant/spring-boot-service    项目地址: ...

  6. go通过第三方库 mahonia gbk 转utf8

    go get github.com/axgle/mahonia dec := mahonia.NewDecoder("GBK")ret:=dec.ConvertString(res ...

  7. js设计模式:工厂模式、构造函数模式、原型模式、混合模式

    一.js面向对象程序 var o1 = new Object();     o1.name = "宾宾";     o1.sex = "男";     o1.a ...

  8. 将Render博客搬至GIT(偷懒)

    SmallEngine 一个特别小的研究引擎[用于各种实验] 框架上设计上采用Unreal.Unity的设计思路[偷懒了] https://github.com/daozhangXDZ/DZSmall ...

  9. docker创建redis镜像

    pull redis 镜像 创建redis的镜像有几种方式,可以直接从仓库中拉取,也可以采用dockerfile文件自己编译创建. 基于已有的redis镜像,docker可以采用run,或者creat ...

  10. Balanced Lineup:线段树:区间最值 / RMQ

    不要被线段树这个名字和其长长的代码吓到. D - Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ...