ZOJ 3209
精确覆盖
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int maxn=920;
const int maxnode=920*550;
const int maxr=550;
int ans;
struct DLX
{
int n , sz; // 行数,节点总数
int S[maxn]; // 各列节点总数
int row[maxnode],col[maxnode]; // 各节点行列编号
int L[maxnode],R[maxnode],U[maxnode],D[maxnode]; // 十字链表 int ansd; // 解 void init(int n )
{
this->n = n ;
for(int i = 0 ; i <= n; i++ )
{
U[i] = i ;
D[i] = i ;
L[i] = i - 1;
R[i] = i + 1;
}
R[n] = 0 ;
L[0] = n;
sz = n + 1 ;
memset(S,0,sizeof(S));
}
void addRow(int r,vector<int> c1)
{
int first = sz;
for(int i = 0 ; i < c1.size(); i++ ){
int c = c1[i];
L[sz] = sz - 1 ; R[sz] = sz + 1 ; D[sz] = c ; U[sz] = U[c];
D[U[c]] = sz; U[c] = sz;
row[sz] = r; col[sz] = c;
S[c] ++ ; sz ++ ;
}
R[sz - 1] = first ; L[first] = sz - 1;
}
// 顺着链表A,遍历除s外的其他元素
#define FOR(i,A,s) for(int i = A[s]; i != s ; i = A[i]) void remove(int c){
L[R[c]] = L[c];
R[L[c]] = R[c];
FOR(i,D,c)
FOR(j,R,i) {U[D[j]] = U[j];D[U[j]] = D[j];--S[col[j]];}
}
void restore(int c){
FOR(i,U,c)
FOR(j,L,i) {++S[col[j]];U[D[j]] = j;D[U[j]] = j; }
L[R[c]] = c;
R[L[c]] = c;
}
void dfs(int d){
if(d>=ans) return ;
if(R[0] == 0 ){
ansd = d;
ans=min(ans,ansd);
}
// 找S最小的列c
int c = R[0] ;
FOR(i,R,0) if(S[i] < S[c]) c = i; remove(c);
FOR(i,D,c){
FOR(j,R,i) remove(col[j]);
dfs(d + 1);
FOR(j,L,i) restore(col[j]);
}
restore(c);
}
bool solve(){
dfs(0);
}
}; DLX solver; int main(){
int T; int n,m ,p; int xx1,xx2,yy1,yy2;
vector<int>colmuns;
scanf("%d",&T);
while(T--){
ans=(1<<30);
scanf("%d%d%d",&n,&m,&p);
solver.init(n*m);
for(int k=1;k<=p;k++){
colmuns.clear();
scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
for(int i=xx1+1;i<=xx2;i++){
for(int j=yy1+1;j<=yy2;j++){
colmuns.push_back((i-1)*m+j);
}
}
solver.addRow(k,colmuns);
}
solver.solve();
if(ans==(1<<30)) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
ZOJ 3209的更多相关文章
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- zoj 3209.Treasure Map(DLX精确覆盖)
直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- zoj - 3209 - Treasure Map(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。
Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...
- ZOJ 3209 Treasure Map(精确覆盖)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...
- ZOJ 3209 Treasure Map DLX
用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...
随机推荐
- 查看服务器wwn是否在交换机侧
判断port_state是否为Online状态,是的话,读取出port_name,即为wwn. #!/usr/bin/env python3 # -*- coding: UTF-8 -*- impor ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- div自动适应浏览器窗口水平和垂直居中
html <body> <div id="centerDiv">自动适应水平和垂直居中</div> </body> css ;;} ...
- 【Oracle】体系结构
1. 理解实例和数据库 ☞ 实例是一组后台进程和共享内存 ☞ 数据库是磁盘上存储的数据集合 ☞ 实例“一生”只能装载并打开一个数据库 ☞ 数据库可以由一个或多个实例(RAC)装载和打开 [oracle ...
- 用SQL Server查询所有数据并显示
利用SQL Server查询数据,并把所有数据显示在页面上. "%> <%@page import="jdbc.sqlServer"%> <%@p ...
- Spring AOP之静态代理
软件151 李飞瑶 一.SpringAOP: ⒈AOP:Aspect Oriented Programming 面向切面编程, 实现的是核心业务和非核心业务之间的的分离,让核心类只做核心业务,代理类只 ...
- LINQ to Entities 不识别方法“System.Nullable`1[System.Int32] DiffDays(System.Nullable`1[System.DateTime], System.Nullable`1[System.DateTime])”,因此该方法无法转换为存储表达式。
解决方案: db.table.Where(m=>System.Data.Objects.EntityFunctions.DiffDays(m.CreateTime, DateTime.Now) ...
- es7 class的写法
再看vue-router源码的时候(代码链接)看到这样的代码片段: export default class VueRouter { app: any; apps: Array<any>; ...
- js-url解析函数
//Url解析 function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: ...
- drf03 drf视图中提供的请求类和响应类
drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. Django REST ...