对于可以重复覆盖的DLX,我们在选择一行,删除的时候是这样的(解读kuangbin模板)
void remove(int c) //从c开始,往下,把所有元素都和左右隔离开
{
for(int i = D[c];i != c;i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
对于要删c元素,把除了c元素以外所有的这一列上的元素,都和所在行分割开。
这样,显然如果可以覆盖的话,第一行的标记一定切割了。(就是图中,c最上面的那个,也被左右方向隔开了)
然后继续“跳舞”,去删其他元素。
这个图。。。去删c左右方向的节点。先删黄色的,用黄色节点执行上面的函数,然后黄色最上方的红色节点被删去。
再删绿的,绿色最上方的红色节点被删去。
这样,我删了c以后(重复覆盖的选择了第二行),还可以选第三行,但是删第三行玩的时候,就可以跳过一些节点不用删了。大大提升了效率。
剪枝: 对于求最少删除方案,我们可以最一个最好的估价。
估价方法:
假设我们要覆盖第i列,我们一次选择所有能覆盖第i列的行(因为我们只有这么多选项),这样就又有很多列被覆盖了。找到下一个没有被覆盖的列,还是一次选择所有能覆盖这一列的行,然后继续下去。
虽然一次选择多行,但是我们记为一次选择。 这样的总选择数,就是最好的结果。
然后用当前步数+估价,和当前得到最优答案进行比较,判断剪枝即可
//#include <bits/stdc++.h>
//#include <ext/pb_ds/priority_queue.hpp>
//#include <tr1/unordered_map>
//#include <ctime>
//using std::tr1::unordered_map;
#include <iostream>
/*
using std::sort;
using std::bitset;
using std::max;
using std::cout;
using std::stack;
using std::cin;
using std::endl;
using std::swap;
using std::pair;
using std::vector;
using std::set;
using std::map;
using std::multiset;
using std::queue;
using std::greater;
using std::string;
using std::priority_queue;
using std::max_element;
using std::min_element;
*/
//using __gnu_pbds::pairing_heap_tag;
//__gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> heap;
#define Hash unordered_map
#define pr(x) cout<<#x<<" = "<<x<<" "
#define prln(x) cout<<#x<<" = "<<x<<endl
int n, m;
const int MaxM = 15*15+10;
const int MaxN = 15*15+10;
const int maxnode = MaxN * MaxM;
const int INF = 0x3f3f3f3f;
struct DLX
{
/*
* 可重复覆盖,每次为删除一行
*/
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];//maxnode表示总的节点数量
int H[MaxN],S[MaxM];
int ansd;
void init(int _n,int _m) //初始化为n行,m列
{
n = _n;
m = _m;
for(int i = 0;i <= m;i++)
{
S[i] = 0; //i列元素数量,这里初始化为0
U[i] = D[i] = i;
L[i] = i-1;
R[i] = i+1;
}
R[m] = 0; L[0] = m;
size = m; //总元素数量,用来做数组用
for(int i = 1;i <= n;i++)H[i] = -1; //每一行的H为空
ansd = INF;
}
void Link(int r,int c) //r行,c列有元素,添加这个元素
{
++S[Col[++size]=c];//S[c]++表示c列元素数量增加, col[k]表示第k个元素是第几列
Row[size] = r; //row[k]第k个元素是第r行
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size; //链表的互相操作
if(H[r] < 0)H[r] = L[size] = R[size] = size; //更改行的相关指针
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
void remove(int c) //从c开始,往下,把所有元素都和左右隔离开
{
for(int i = D[c];i != c;i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
void resume(int c) //恢复第c列
{
for(int i = U[c];i != c;i = U[i])
L[R[i]] = R[L[i]] = i;
}
bool v[MaxM];
int f() //剪枝,至少还要选择多少次,才能全部选择完
{
/*思想:对于一个没有被选择的列,如果选择这一列的时候,能把所有和这一列相关的列都选中,*/
int ret = 0;
for(int c = R[0]; c != 0;c = R[c])v[c] = true;//表示c列还没有删
for(int c = R[0]; c != 0;c = R[c])
if(v[c]) //如果c列没有被删
{
ret++; //至少要选一个,但是我们把所有的都给选上了,这一定是最少的方案
v[c] = false;
for(int i = D[c];i != c;i = D[i])//[1...m]都是初始点!,所以一开始从D[c]开始
for(int j = R[i];j != i;j = R[j])
v[Col[j]] = false;
}
return ret;
}
void Dance(int d)
{
//if(d + f() >= ansd)return;
if(R[0] == 0)
{
if(d < ansd)ansd = d;
return;
}
int c = R[0];
for(int i = R[0];i != 0;i = R[i])//找到第c列,还没有被消除的
if(S[i] < S[c])
c = i;
for(int i = D[c];i != c;i = D[i]) //穷举消除
{
remove(i);//第c列第i行
for(int j = R[i];j != i;j = R[j])remove(j); //删除这一行,因为是可重复的,所以值要删这一行的所有元素,同时这一行元素的列也删去
Dance(d+1);
for(int j = L[i];j != i;j = L[j])resume(j);//恢复
resume(i);
}
}
}dlx;
char s[200];
int mp[20][20], a[20][20];
int x, y;
int guaishou;
void makeit(int p, int row, int col)
{
for (int i = row; i <= row + x - 1; ++ i)
for (int j = col; j <= col + y - 1; ++ j)
{
if (a[i][j] == 1) dlx.Link(p, mp[i][j]);
}
}
void init()
{
guaishou=0;
for (int i = 1; i <= n;++i)
for (int j = 1; j <= m;++j)
{
scanf("%d", &a[i][j]);
if (a[i][j])
{ ++guaishou;
mp[i][j] = guaishou;
}
}
scanf("%d%d", &x, &y);
dlx.init(n * m, guaishou);
int sb=0;
for (int i = 1; i + x - 1<= n; ++ i)
for (int j = 1; j + y - 1 <= m; ++ j)
{
++sb;
makeit(sb, i, j);
}
}
int main()
{
int t=0;
for (int i = 1; i <= 15;++i)
for (int j = 1; j <= 15; ++j)
if (mp[i][j] = ++t);
while (~scanf("%d%d", &n, &m))
{
init();
dlx.Dance(0);
printf("%d\n", dlx.ansd);
}
return 0;
}