题目:
http://acm.fzu.edu.cn/problem.php?pid=1686
题意:
Problem Description
这是个剑与魔法的世界.英雄和魔物同在,动荡和安定并存.但总的来说,库尔特王国是个安宁的国家,人民安居乐业,魔物也比较少.但是.总有一些魔物不时会进入城市附近,干扰人民的生活.就要有一些人出来守护居民们不被魔物侵害.魔法使艾米莉就是这样的一个人.她骑着她的坐骑,神龙米格拉一起消灭干扰人类生存的魔物,维护王国的安定.艾米莉希望能够在损伤最小的前提下完成任务.每次战斗前,她都用时间停止魔法停住时间,然后米格拉他就可以发出火球烧死敌人.米格拉想知道,他如何以最快的速度消灭敌人,减轻艾米莉的负担.
Input
数据有多组,你要处理到EOF为止.每组数据第一行有两个数,n,m,(1<=n,m<=15)表示这次任务的地区范围. 然后接下来有n行,每行m个整数,如为1表示该点有怪物,为0表示该点无怪物.然后接下一行有两个整数,n1,m1 (n1<=n,m1<=m)分别表示米格拉一次能攻击的行,列数(行列不能互换),假设米格拉一单位时间能发出一个火球,所有怪物都可一击必杀.
Output
输出一行,一个整数,表示米格拉消灭所有魔物的最短时间.
思路:
重复覆盖简单题,给每个怪物编号,作为列约束,把不同的攻击位置作为行
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int X = 100000 + 10, N = 250, M = 250 + 10, INF = 0x3f3f3f3f;
int g[20][20];
int ans;
struct DLX
{
int U[X], D[X], L[X], R[X], row[X], col[X];
int H[N], S[M];
int head, sz, tot, n, m;
bool vis[M];
void init(int _n, int _m)
{
n = _n, m = _m;
for(int i = 0; i <= m; i++)
L[i] = i-1, R[i] = i+1, U[i] = D[i] = i, S[i] = 0;
head = 0, tot = 0, sz = m;
L[head] = m, R[m] = head;
for(int i = 1; i <= n; i++) H[i] = -1;
}
void link(int r, int c)
{
++S[col[++sz]=c];
row[sz] = r;
D[sz] = D[c], U[D[c]] = sz;
U[sz] = c, D[c] = sz;
if(H[r] < 0) H[r] = L[sz] = R[sz] = sz;
else R[sz] = R[H[r]], L[R[H[r]]] = sz, L[sz] = H[r], R[H[r]] = sz;
}
void del(int x)
{
for(int i = D[x]; i != x; i = D[i])
R[L[i]] = R[i], L[R[i]] = L[i];
}
void recover(int x)
{
for(int i = U[x]; i != x; i = U[i])
R[L[i]] = L[R[i]] = i;
}
int fun_f()
{
memset(vis, 0, sizeof vis);
int num = 0;
for(int i = R[head]; i != head; i = R[i])
if(! vis[i])
{
vis[i] = true;
num++;
for(int j = D[i]; j != i; j = D[j])
for(int k = R[j]; k != j; k = R[k])
vis[col[k]] = true;
}
return num;
}
void dance(int dep)
{
if(R[head] == head)
{
ans = min(ans, dep-1);
}
if(dep-1 + fun_f() >= ans) return;
int c = R[head];
for(int i = R[head]; i != head; i = R[i])
if(S[i] < S[c]) c = i;
for(int i = D[c]; i != c; i = D[i])
{
del(i);
for(int j = R[i]; j != i; j = R[j]) del(j);
dance(dep + 1);
for(int j = L[i]; j != i; j = L[j]) recover(j);
recover(i);
}
}
}dlx;
int main()
{
int n, m, lenx, leny;
while(~ scanf("%d%d", &n, &m))
{
int num = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d", &g[i][j]);
if(g[i][j]) g[i][j] = ++num;
}
scanf("%d%d", &lenx, &leny);
dlx.init((n-lenx+1) * (m-leny+1), num);
for(int i = lenx; i <= n; i++)
for(int j = leny; j <= m; j++)
for(int k = i-lenx+1; k <= i; k++)
for(int l = j-leny+1; l <= j; l++)
if(g[k][l] != 0)
{
int r = (i-lenx+1-1) * (m-leny+1) + (j-leny+1);
int c = g[k][l];
dlx.link(r, c);
}
ans = INF;
dlx.dance(1);
printf("%d\n", ans);
}
return 0;
}