思路
- 在一个nm的0、1矩阵中,1表示怪物,神龙的每次攻击可以消灭一个 n1m1的矩形的内的怪物,问消灭地图中的所有怪物最少需要多少次攻击?
- DLX重复覆盖 解决问题
- 构造数据矩阵
- 给怪物编上编号作为列号,从1开始,把怪物当做列
- 把神龙的攻击区域当做行,神龙的攻击区域有很多选择,但是由于数据范围小,我们直接暴力枚举 所有攻击区域,在枚举的时候也给这些当做行的区域编上编号作为行号,对于某个攻击区域(对应的行的编号为i),如果这个区域内有怪物(怪物编号为j),那么对应的数据矩阵(i,j)位置上为1,否则为0,
- 构造出数据矩阵之后,之间上模版就行了
代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define db double
#define Pir pair<int, int>
#define PIR pair<Pir, Pir>
#define m_p make_pair
#define INF 0x3f3f3f3f
#define esp 1e-7
#define mod (ll)(1e9 + 7)
#define for_(i, s, e) for(int i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(int i = (ll)(e); i >= (ll)(s); i --)
#define sc scanf
#define sd(a) scanf("%d", &a)
#define ss(a) scanf("%s", a)
using namespace std;
const int maxn=250;
const int maxm=250;
const int maxnode=250*250;
int n,m,n1,m1;
int mz[20][20];
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[maxn],S[maxn];
int ansd,ans[maxn];
void init(int _n)
{
n = _n;
for_(i, 0, n)
{
L[i] = i - 1;
R[i] = i + 1;
U[i] = D[i] = i;
S[i] = 0;
}
R[n] = 0, L[0] = n, size = n;
memset(H, -1, sizeof(H));
ansd = INF;
}
void link(int r, int c)
{
Col[++ size] = c;
Row[size] = r;
S[c] ++;
U[size] = U[c];
D[size] = c;
D[U[c]] = size;
U[c] = size;
if(H[r] == -1)
H[r] = L[size] = R[size] = size;
else
{
L[size] = size - 1;
R[size] = H[r];
R[L[size]] = size;
L[H[r]] = size;
}
}
void remove(int 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)
{
for(int i = U[c]; i != c; i = U[i])
L[R[i]] = i, R[L[i]] = i;
}
bool v[maxnode];
int f()
{
int ret=0;
for(int c=R[0];c!=0;c=R[c])
v[c]=true;
for(int c=R[0];c!=0;c=R[c])
if(v[c])
{
ret++;
v[c]=false;
for(int i=D[c];i!=c;i=D[i])
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)
{
ansd = min(ansd, d);
return;
}
int c = R[0];
for(int i = R[0]; i; i = R[i])
if(S[c] > S[i])
c = i;
for(int i = D[c]; i != c; i = D[i])
{
remove(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;
int main()
{
while(~ sc("%d %d", &m, &n))
{
int hang = 0, lie = 0;
for_(i, 1, m)
for_(j, 1, n)
{
sd(mz[i][j]);
if(mz[i][j])
mz[i][j] = ++lie;
}
int x, y;
sc("%d %d", &x, &y);
dlx.init(lie);
for_(i, 1, m - x + 1)
for_(j, 1, n - y + 1)
{
++ hang;
for_(a, i, i + x - 1)
for_(b, j, j + y - 1)
if(mz[a][b])
dlx.link(hang, mz[a][b]);
}
dlx.dance(0);
printf("%d\n", dlx.ansd);
}
return 0;
}