$$\color{Purple}{kuangbin题解目录}$$
描述
给定一个 $n \\times m$ 的 $01$ 矩阵。
每次操作,你可以选择一个 $h \\times w$ 的子矩阵,并将其中的所有 $1$ 变为 $0$。
请问,想要将矩阵变为全 $0$ 矩阵,至少需要进行多少次操作。
输入格式
输入包含多组测试数据。
每组数据第一行包含两个整数 $n,m$。
接下来 $n$ 行,每行包含 $m$ 个整数 $0$ 或 $1$,用来描述整个 $01$ 矩阵。
最后一行,包含两个整数 $h,w$。
输出格式
一个整数,表示最少所需操作次数。
数据范围
最多包含 $100$ 组数据。
$1 \\le n,m \\le 15$,
$1 \\le h \\le n$,
$1 \\le w \\le m$。
输入样例:
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
2 2
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
2 2
输出样例:
4
1
思路
- 可用
重复覆盖问题
解决该题.- 该$01$矩阵,将每一个小矩阵当作其横坐标(最坏情况下有$n\times m$个$1\times 1$的小矩阵),用$1$的个数作为其纵坐标(最坏情况下全是$1$,即有$n\times m$列),当小矩阵中存在$1$的元素时插入结点即可.
- 用
估价函数
来剪枝.
代码
// Problem: 神龙的难题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/4239/
// Memory Limit: 64 MB
// Time Limit: 2000 ms
// Date: 2023-01-14 17:33:54
//
// Powered by CP Editor (https://cpeditor.org)
/**
* @author : SDTBU_LY
* @version : V1.0.0
* @上联 : ac自动机fail树上dfs序建可持久化线段树
* @下联 : 后缀自动机的next指针DAG图上求SG函数
**/
#include<iostream>
#include<cstring>
#include<algorithm>
#define MAXN 500
#define MAXM 250000
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int n,m;
int g[MAXN][MAXN];
int id[MAXN][MAXN];
struct Dancing_Links_Node{
int Up;
int Down;
int Left;
int Right;
int row;//行数
int col;//列数
};
int cnt=0;//记录结点个数
Dancing_Links_Node node[MAXM];
int f_row[MAXN];//记录每行的第一个元素
int c_cnt[MAXN];//每列元素个数
int res;
int vis[MAXN];
int f()
{
int res=0;
memset(vis,0,sizeof(vis));
for(int i=node[0].Right;i!=0;i=node[i].Right)
if(vis[i]==0)
{
vis[i]=1;
res++;
for(int j=node[i].Down;j!=i;j=node[j].Down)
for(int k=node[j].Right;k!=j;k=node[k].Right)
vis[node[k].col]=1;
}
return res;
}
void Init_Node(int num)//初始化head和列元素,将其上下左右指针指好,行数和列数可不用初始化
{
for(int i=0;i<=num;i++)
{
node[i].Left=i-1;//左-1
node[i].Right=i+1;//右+1
node[i].Up=i;//上下指向自己
node[i].Down=i;
c_cnt[i]=0;//列个数清空
}
node[0].Left=num;//让head的左边指向最后一个的列元素
node[num].Right=0;//让最后一个的列元素的右边指向head
cnt=num;//更新结点个数
res=inf;
memset(f_row,-1,sizeof(f_row));
}
void Insert_Node(int x,int y){//插入新节点(按顺序放)
//更新具体值
node[++cnt].row=x;//更新行数
node[cnt].col=y;//更新列数
//先处理该结点的所在列链上的情况
node[cnt].Down=node[y].Down;//更新当前元素的指向下面
node[node[y].Down].Up=cnt;//之前的最后一个元素指向最后
node[cnt].Up=y;
node[y].Down=cnt;
//然后处理该结点的所在行链上的情况
if(f_row[x]<0)//表示该结点是其所在行的第一个元素
{
node[cnt].Left=cnt;//自己指向自己
node[cnt].Right=cnt;//自己指向自己
f_row[x]=cnt;//更新当前行的第一个元素
}
else//表示该结点不是其所在行的第一个元素
{
node[cnt].Right=node[f_row[x]].Right;
node[node[f_row[x]].Right].Left=cnt;
node[cnt].Left=f_row[x];
node[f_row[x]].Right=cnt;
}
c_cnt[y]++;//当前列元素+1
}
//与精准覆盖的差别在于删除、恢复和dance函数
void Remove_Link(int y)//删除该列
{
for(int i=node[y].Down;i!=y;i=node[i].Down)
{
node[node[i].Right].Left=node[i].Left;
node[node[i].Left].Right=node[i].Right;
}
}
void Resume_Link(int y)//恢复该列
{
for(int i=node[y].Up;i!=y;i=node[i].Up)//枚举列链中的元素
{
node[node[i].Right].Left=i;
node[node[i].Left].Right=i;
}
}
void dance(int depth)//depth表示答案的个数(所搜的层数)
{
if(f()+depth>=res)//剪枝
return ;
if(node[0].Right==0)//如果head.right=head,说明有解,输出答案
{
res=depth;
return ;
}
int y=node[0].Right;//取列元素y=head.right
for(int i=node[0].Right;i!=0;i=node[i].Right)//剪枝(减少搜索树的分叉)
if(c_cnt[i]<c_cnt[y])
y=i;
for(int i=node[y].Down;i!=y;i=node[i].Down)
{
Remove_Link(i);//注不能是node[i].col
for(int j=node[i].Right;j!=i;j=node[j].Right)
Remove_Link(j);
dance(depth+1);
//先右后左
for(int j=node[i].Left;j!=i;j=node[j].Left)
Resume_Link(j);
Resume_Link(i);
}
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0),cout.tie(0);
while(cin >> n >> m)
{
int idx=0;
int h,w;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin >> g[i][j];
if(g[i][j]==1)
id[i][j]=++idx;
}
Init_Node(idx);
cin >> h >> w;
int tot_row=0;
for(int x=1;x+h-1<=n;x++)
for(int y=1;y+w-1<=m;y++)
{
tot_row++;
for(int i=x;i<=x+h-1;i++)
for(int j=y;j<=y+w-1;j++)
if(g[i][j]==1)
Insert_Node(tot_row,id[i][j]);
}
//cout << tot_row << endl;
dance(0);
cout << res << endl;
}
return 0;
}
重复覆盖问题,学到了