用最少的矩阵覆盖n*m的地图。注意矩阵不能互相覆盖。

这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
using namespace std;
const int maxnode = 550005;
const int MaxM = 999;
const int MaxN = 555;
int K,n,m;
void print(int x)
{
printf("(%d %d)\n",x%(n+1)==0? n:(x%(n+1)-1),x%(n+1)==0? x/(n+1)-1:x/(n+1));
}
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN],S[MaxM];
int ands,ans[MaxN],ANS;
void init(int _n,int _m)
{
ANS=0x3f3f3f3f;
n = _n;
m = _m;
for(int i = 0; i <= m; i++)
{
S[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;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size] = 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)
{
int i,j;
L[R[c]]=L[c];
R[L[c]]=R[c];
for(i=D[c]; i!=c; i=D[i])
{
for(j=R[i]; j!=i; j=R[j])
{
U[D[j]]=U[j],D[U[j]]=D[j];
S[Col[j]]--;
}
}
}
void resume(int c)
{
int i,j;
for(i=U[c]; i!=c; i=U[i])
{
for(j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
L[R[c]]=c;
R[L[c]]=c;
}
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()>=ANS) return;
if(R[0] == 0)
{
ANS=min(ANS,d);
return;
}
int c = R[0];
for(int i = R[0]; i != 0; i = R[i])
if(S[i] < S[c])
c = i;
remove(c);
for(int i = D[c]; i != c; i = D[i])
{
for(int j = R[i]; j != i; j = R[j])remove(Col[j]);
Dance(d+1);
for(int j = L[i]; j != i; j = L[j])resume(Col[j]);
}
resume(c);
}
} g;
int main()
{
int cas,x1,y1,x2,y2,k;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d",&n,&m,&k);
g.init(k,n*m);
for(int q=1; q<=k; q++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i = x1+1; i <= x2; i++)
for(int j = y1+1; j <= y2; j++)
g.Link(q,j+(i-1)*m);
}
g.Dance(0);
if(g.ANS==0x3f3f3f3f) g.ANS=-1;
printf("%d\n",g.ANS);
}
return 0;
}

最新文章

  1. [Effective Sublime Text] (01) Sublime Text 3 开发环境初始化
  2. 我的Hcharts的页面应用
  3. BZOJ 2431 &amp; DP
  4. ros机器人开发概述
  5. ImageView
  6. flow.ci Beta 上线,将开发工作流自动化
  7. C语言中的atan和atan2(转)
  8. html asp php java 清除缓存
  9. UPDATE sql 优化
  10. wpf Content数据绑定StringFormat起作用的原理和解决(转)
  11. 无线wifi-PJ-之在开启WPS下使用reaver
  12. SCM文章4教训:定时器共阴极LED动态显示屏
  13. Excel教程(4) - 数据库函数
  14. 监控 redis 执行命令
  15. Day 6-1计算机网络基础&amp;TCP/IP
  16. Windows平台Mysql表名变小写的解决过程
  17. MarkDown编辑器中缩进
  18. mysql binlog to sql and show mysqlstatusadmin
  19. Web API 令牌(秘钥是双方约定的,并不在网络连接上传输)
  20. 【转】javascript深入理解js闭包

热门文章

  1. 3.lombok系列3:lombok的实验类特性
  2. Spark应用程序部署工具Spark Submit
  3. Vue自定义组件
  4. robotframework Selenium2+RFS自动化测试
  5. JSP中的EL表达式详细介绍
  6. Android之——图片的内存优化
  7. 下拉列表,点击选择实现跳转链接 onchange=&quot;window.location=...&quot;
  8. Altium Designer如何统一改变pcb状态下的原件标号位置
  9. 【习题 3-7 UVA - 1368 】DNA Consensus String
  10. 基于StringUtils工具类的常用方法介绍(必看篇)