Description

  The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The lengths of all matchsticks are one. You can find many squares of different sizes in the grid. The size of a square is the length of its side. In the grid shown in the left figure, there are 9 squares of size one, 4 squares of size two, and 1 square of size three.

  Each matchstick of the complete grid is identified with a unique number which is assigned from left to right and from top to bottom as shown in the left figure. If you take some matchsticks out from the complete grid, then some squares in the grid will be destroyed, which results in an incomplete 3*3 grid. The right figure illustrates an incomplete 3*3 grid after removing three matchsticks numbered with 12, 17 and 23. This removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. Consequently, the incomplete grid does not have squares of size three, but still has 4 squares of size one and 1 square of size two. 

  As input, you are given a (complete or incomplete) n*n grid made with no more than 2n(n+1) matchsticks for a natural number 5 <= n . Your task is to compute the minimum number of matchsticks taken 
  out to destroy all the squares existing in the input n*n grid.

 
  DLX可重复覆盖的问题,让我们去掉几根火柴,然后把所有的正方形都破坏掉。。。
  把每一个正方形都当做是一列,然后每一根火柴当做是一行。
 
  其中对于列和行的构造是麻烦的地方。。。。。。
  我是枚举每一个正方形,然后枚举这个正方形的每一条边。。。。。。
  
  其次就是Link之前要删除掉几行,这里TLE了N次,因为删除的时候要用到row,而且H[r]在某些情况下也应该改变才对(这里忘记了,导致一些数据循环停不下来。。。。。。)
 
代码如下:
#include<iostream>
#include<cstring> using namespace std; const int INF=10e8;
const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; struct DLX
{
int L[MaxNode],R[MaxNode],U[MaxNode],D[MaxNode],col[MaxNode],row[MaxNode];
int S[MaxM],H[MaxN];
int n,m,size;
int ans; void init(int _n,int _m)
{
n=_n;
m=_m; for(int i=;i<=m;++i)
{
U[i]=D[i]=i;
R[i]=i+;
L[i]=i-;
row[i]=; // !!! S[i]=;
} R[m]=;
L[]=m; size=m;
ans=INF; for(int i=;i<=n;++i) // !!!
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
++S[c];
row[size]=r; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
for(int i=D[c];i!=c;i=D[i])
{
R[L[i]]=R[i];
L[R[i]]=L[i];
}
} void remove1(int r)
{
if(H[r]==-)
return; for(int i=U[H[r]];i!=H[r];i=U[i])
{
if(H[row[i]]==i) // !!!
{
if(R[i]==i)
H[row[i]]=-;
else
H[row[i]]=R[i];
} L[R[i]]=L[i];
R[L[i]]=R[i];
} for(int i=R[H[r]];i!=H[r];i=R[i])
for(int j=U[i];j!=i;j=U[j])
{
if(H[row[j]]==j)
{
if(R[j]==j)
H[row[j]]=-;
else
H[row[j]]=R[j];
} L[R[j]]=L[j];
R[L[j]]=R[j];
}
} void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
R[L[i]]=L[R[i]]=i;
} bool vis[MaxM]; int getH()
{
int ret=; for(int c=R[];c!=;c=R[c])
vis[c]=; for(int c=R[];c!=;c=R[c])
if(vis[c])
{
++ret;
vis[c]=; for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
vis[col[j]]=;
} return ret;
} void Dance(int d)
{
if(d+getH()>=ans)
return; if(R[]==)
{
if(d<ans)
ans=d; return;
} int c=R[]; for(int i=R[];i!=;i=R[i])
if(S[i]<S[c])
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+); for(int j=L[i];j!=i;j=L[j])
resume(j); resume(i);
}
}
}; int N;
DLX dlx;
int ans1[]={,,,,,}; void slove()
{
dlx.init(*N*(N+),N*(N+)*(*N+)/); int t1,t2;
int cou=;
int K,a; cin>>K; if(K==)
{
cout<<ans1[N]<<endl;
return;
} for(int i=;i<=N;++i)
for(int j=;j<=(N-i+)*(N-i+);++j)
{
++cou; t1=(j-)%(N-i+)++(*N+)*((j-)/(N-i+)); for(int k=;k<i;++k)
{
dlx.Link(k+t1,cou);
dlx.Link((*N+)*k+t1+i-+N+,cou);
dlx.Link((*N+)*k+t1+N,cou);
dlx.Link(k+t1+i*(*N+),cou);
}
} for(int i=;i<K;++i)
{
cin>>a; dlx.remove1(a);
} dlx.Dance(); if(dlx.ans==INF)
cout<<<<endl;
else
cout<<dlx.ans<<endl;
} int main()
{
ios::sync_with_stdio(false); int T;
cin>>T; while(T--)
{
cin>>N; slove();
} return ;
}

(中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。的更多相关文章

  1. UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)

    题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...

  2. [DLX反复覆盖] poj 1084 Square Destroyer

    题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...

  3. (中等) HDU 5046 Airport ,DLX+可重复覆盖+二分。

    Description The country of jiuye composed by N cites. Each city can be viewed as a point in a two- d ...

  4. (简单) FZU 1686 神龙的难题 , DLX+可重复覆盖。

    Description 这是个剑与魔法的世界.英雄和魔物同在,动荡和安定并存.但总的来说,库尔特王国是个安宁的国家,人民安居乐业,魔物也比较少.但是.总有一些魔物不时会进入城市附近,干扰人民的生活.就 ...

  5. HDU 3957 Street Fighter(搜索、DLX、重复覆盖+精确覆盖)

    很久以前就看到的一个经典题,一直没做,今天拿来练手.街霸 给n<=25个角色,每个角色有 1 or 2 个版本(可以理解为普通版以及爆发版),每个角色版本可以KO掉若干人. 问最少选多少个角色( ...

  6. poj1084Square Destroyer(LDX解重复覆盖)

    题目请戳这里 题目大意:给一个n*n的用单位长度的木棍拼起来的网格图,给每个木棍按图示编号,编号范围1~2*n*(n+1).现在已知图中已经去掉了k个木棍,求还要至少去掉几根木棍能使网格图中不存在正方 ...

  7. 【POJ】1084 Square Destroyer

    1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...

  8. hdu5064 DLX可重复覆盖+二分

    这题题意是 给了n个城市 在其中小于等于k个城市建立机场然后 使得最远的那个离机场的城市距离最短 二分答案 ,我们对于每次的mid 重新建图然后再来一次DLX,每个点可以覆盖的点建立一条联系就ok了 ...

  9. (中等) HDU 3335 , DLX+重复覆盖。

    Description As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory. ...

随机推荐

  1. J2EE判断重复的数据

    import java.util.ArrayList; import java.util.List; import org.junit.Test; /** * 判断重复的数据 * @author Ya ...

  2. c语言库函数

    #include <stdio.h> #define LENGTH 100 main() { FILE *fd; char str[LENGTH]; fd = fopen("he ...

  3. ignite通过注解配置查询

    官方文档的叙述可能有些不清楚,我做了一个测试,并且可以成功运行,待会儿后面贴出小栗子. 两步操作: 第一步在属性值处贴上@QuerySqlField注解 第二部设置key和value类型 Person ...

  4. POJ 1308/并查集

    题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...

  5. javascript语句语义大全(5)

    1. var str = "abcd";alert(str.length);alert(str.charAt(0));//获取下标为0的字符alert(str.charCodeAt ...

  6. 多校 Robot Motion

    题目链接:http://acm.hust.edu.cn/vjudge/contest/124435#problem/J 密码:acm Sample Input NEESWE WWWESS SNWWWW ...

  7. Java8学习笔记----Lambda表达式 (转)

    Java8学习笔记----Lambda表达式 天锦 2014-03-24 16:43:30 发表于:ATA之家       本文主要记录自己学习Java8的历程,方便大家一起探讨和自己的备忘.因为本人 ...

  8. Effective java -- 4 泛型

    第二十三条:请不要在代码中使用原生态类型就是像Set这种待泛型的,就把泛型明确写出来. 第二十四条:消除非受检警告就是Set<String> sets = new HashSet();这种 ...

  9. heartbeat集群安装配置

    安装配置高可用集群需要注意:1.节点名称:集群每个节点的名称都得能互相解析 /etc/hosts hosts主机名的正反解析结果必须跟"uname -n"的结果保持一致2.时间必须 ...

  10. 英文SEO外部链接资源收集之常用的footprints

      inurl:/privacy-policy "Using Article Directory plugin"inurl:/terms "Using Article D ...