[DLX反复覆盖] poj 1084 Square Destroyer
题意:
n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉。
求在已经拿走一些火柴的情况下。还须要拿走至少多少根火柴能够把全部的正方形都破坏掉。
思路:
对于每一个位置遍历全部可能的边长,确定这个边长下的正方形的边相应的都是数字几,而且把正方形从1開始编号。
然后依据编号,把正方形和数字建边记录方便以下建图。
然后以火柴棍为行,正方形为列,建立dancing link
然后求解。
这里注意的是,须要强行插入某些行。
代码:
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"cmath"
#include"queue"
#include"map"
#include"vector"
#include"string"
using namespace std;
#define RN 70
#define CN 70
#define N 70*70
int fuck[123];
vector<int>edge[123];
struct DLX
{
int n,m,C;
int U[N],D[N],L[N],R[N],Row[N],Col[N];
int H[RN],S[CN],cnt,ans[RN];
void init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=0; i<=m; i++)
{
S[i]=0;
U[i]=D[i]=i;
L[i]=(i==0?m:i-1);
R[i]=(i==m? 0:i+1);
}
C=m;
for(int i=1; i<=n; i++) H[i]=-1;
}
void link(int x,int y)
{
C++;
Row[C]=x;
Col[C]=y;
S[y]++;
U[C]=U[y];
D[C]=y;
D[U[y]]=C;
U[y]=C;
if(H[x]==-1) H[x]=L[C]=R[C]=C;
else
{
L[C]=L[H[x]];
R[C]=H[x];
R[L[H[x]]]=C;
L[H[x]]=C;
}
}
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 rec(int x)
{
for(int i=U[x]; i!=x; i=U[i])
{
R[L[i]]=i;
L[R[i]]=i;
}
}
int used[CN];
int h()
{
int sum=0;
for(int i=R[0]; i!=0; i=R[i]) used[i]=0;
for(int i=R[0]; i!=0; i=R[i])
{
if(used[i]==0)
{
sum++;
used[i]=1;
for(int j=D[i]; j!=i; j=D[j]) for(int k=R[j]; k!=j; k=R[k]) used[Col[k]]=1;
}
}
return sum;
}
void dance(int x)
{
if(x+h()>=cnt) return ;
if(R[0]==0)
{
cnt=min(cnt,x);
return ;
}
int now=R[0];
for(int i=R[0]; i!=0; i=R[i])
{
if(S[i]<S[now])
now=i;
}
for(int i=D[now]; i!=now; i=D[i])
{
del(i);
for(int j=R[i]; j!=i; j=R[j]) del(j);
dance(x+1);
for(int j=L[i]; j!=i; j=L[j]) rec(j);
rec(i);
}
return ;
}
void DeleteTrick(int r) //强行先放入某行。
{
if(H[r] == -1) return ;
for(int i = D[H[r]]; i != H[r]; i = D[i])
{
if(H[Row[i]] == i)
{
if(R[i] == i)
{
H[Row[i]] = -1;
}
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 = D[i]; j != i; j = D[j])
{
if(H[Row[j]] == j)
{
if(R[j] == j)
{
H[Row[j]] = -1;
}
else
{
H[Row[j]] = R[j];
}
}
L[R[j]] = L[j];
R[L[j]] = R[j];
}
}
}
} dlx;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++) scanf("%d",&fuck[i]);
for(int i=0; i<=70; i++) edge[i].clear();
int sum=0;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
int num=(i-1)*(n+n+1)+j;
int lit=min(n-i+1,n-j+1);
for(int k=1; k<=lit; k++)
{
sum++;
int up,down,left,right;
up=num;
left=num+n;
right=left+k;
down=up+k*(n+n+1);
for(int o=0; o<k; o++)
{
edge[up+o].push_back(sum);
edge[left+o*(n+n+1)].push_back(sum);
edge[right+o*(n+n+1)].push_back(sum);
edge[down+o].push_back(sum);
}
}
}
}
dlx.init(2*n*(n+1),sum); for(int i=1; i<=2*n*(n+1); i++)
{
for(int j=0; j<(int)edge[i].size(); j++) dlx.link(i,edge[i][j]);
}
int fff[123];
memset(fff,0,sizeof(fff));
//for(int j=0; j<(int)edge[1].size(); j++) printf("%d ",edge[1][j]);
//puts("");
for(int i=0; i<m; i++)
{
dlx.DeleteTrick(fuck[i]);
}
dlx.cnt=999;
dlx.dance(0);
printf("%d\n",dlx.cnt);
}
return 0;
}
/*
4
4 10
1 2 3 4 5 6 7 8 9 10
*/
[DLX反复覆盖] poj 1084 Square Destroyer的更多相关文章
- (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。
Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...
- FZU 1686 神龙的难题(DLX反复覆盖)
FZU 1686 神龙的难题 pid=1686" target="_blank" style="">题目链接 题意:中文题 思路:每个1看成列, ...
- FZU 1686 神龙的难题 DLX反复覆盖
DLX反复覆盖: 须要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462 Submit: 1401 Time Limit: 1000 mSec Memory L ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- HDOJ 2828 Lamp DLX反复覆盖
DLX反复覆盖模版题: 每一个开关两个状态.但仅仅能选一个,建2m×n的矩阵跑DLX模版.. .. Lamp Time Limit: 2000/1000 MS (Java/Others) Mem ...
- [DLX反复覆盖] hdu 2828 Lamp
题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...
- HDU 4735 Little Wish~ lyrical step~(DLX , 反复覆盖)
解题思路: DLX 的模板题.反复覆盖. #include <stdio.h> #include <string.h> #include <iostream> #i ...
- 【POJ】1084 Square Destroyer
1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...
- [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)
Problem 1686 神龙的难题 Accept: 444 Submit: 1365 Time Limit: 1000 mSec Memory Limit : 32768 KB Pro ...
随机推荐
- Python中yield函数浅析
带有yield的函数在Python中被称之为generator(生成器),下面我们将使用斐波那契数列来举例说明下该函数:(环境是在Python3.x下) 如何生成斐波那契数列: 斐波那契(Fibon ...
- A7. JVM 垃圾回收收集器(GC 收集器)
[概述] 如果说收集算法是内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.Java 虚拟机规范中对垃圾收集器应该如何实现没有任何规定,因此不同的厂商.不同版本的虚拟机所提供的垃圾处理器都可能会 ...
- thinkphp5验证码处理
1.确定项目目录>vendor>topthink>think-captcha目录存在 2.在config中添加验证码配置 //验证码配置 'captcha' => [ // 验 ...
- [Python3网络爬虫开发实战] 3.1-使用urllib
在Python 2中,有urllib和urllib2两个库来实现请求的发送.而在Python 3中,已经不存在urllib2这个库了,统一为urllib,其官方文档链接为:https://docs.p ...
- js 技巧 (一)
· 事件源对象 event.srcElement.tagName event.srcElement.type · 捕获释放event.srcElement.setCapture(); event ...
- uva 10596 - Morning Walk
Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chitta ...
- CSRF之Ajax请求
A:Ajax提交数据是,携带的CSRF在data中: <form method="POST" action="/csrf.html"> {% csr ...
- 集训第六周 古典概型 期望 D题 Discovering Gold 期望
Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...
- Lucene_Hello(示例)
(1)创建project (2)导入Lucene的核心包 (3)编写代码建立索引 /lucene01/src/cn/hk/lucene/TestIndex.java: package cn.hk.lu ...
- map.keySet()获取map全部的key值
map.keySet()获取map全部的key值 public static String getUrlWithQueryString(String url, Map<String, Str ...