POJ 1084 Square Destroyer

23 篇文章 0 订阅

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.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. 
Each test case consists of two lines: The first line contains a natural number n , not greater than 5, which implies you are given a (complete or incomplete) n*n grid as input, and the second line begins with a nonnegative integer k , the number of matchsticks that are missing from the complete n*n grid, followed by 
k numbers specifying the matchsticks. Note that if k is equal to zero, then the input grid is a complete n*n grid; otherwise, the input grid is an incomplete n*n grid such that the specified k matchsticks are missing from the complete n*n grid.

Output

Print exactly one line for each test case. The line should contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid.

Sample Input

2
2
0
3
3 12 17 23

Sample Output

3

3

拿走最少的火柴使得没有任何一个正方形存在

转换为01矩阵,用dlx重复覆盖解决

#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 1005;
int n, T, m, vis[maxn],tot1,tot2,x;

struct point
{
	int x,y,z;
	point(){};
	point(int x,int y,int z):x(x),y(y),z(z){};
}a[maxn],b[maxn];

inline void read(int &ret)
{
	char c;
	do {
		c = getchar();
	} while (c < '0' || c > '9');
	ret = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		ret = ret * 10 + (c - '0');
}

struct DLX
{
	#define maxn 10005
	#define F(i,A,s) for (int i=A[s];i!=s;i=A[i])
	int L[maxn], R[maxn], U[maxn], D[maxn];
	int row[maxn], col[maxn], ans[maxn], cnt[maxn];
	int n, m, num, sz;

	bool Flag;
	void add(int now, int l, int r, int u, int d, int x, int y)
	{
		L[now] = l;	R[now] = r;	U[now] = u;
		D[now] = d;   row[now] = x;  col[now] = y;
	}
	void reset(int n, int m)
	{
		Flag = false;
		this->n = n;	this->m = m;
		for (int i = 0; i <= m; i++)
		{
			add(i, i - 1, i + 1, i, i, 0, i);
			cnt[i] = 0;
		}
		L[0] = m; 	R[m] = 0; 	sz = m + 1;
	}
	void insert(int x, int y)
	{
		int ft = sz - 1;
		if (row[ft] != x)
		{
			add(sz, sz, sz, U[y], y, x, y);
			U[D[sz]] = sz; D[U[sz]] = sz;
		}
		else
		{
			add(sz, ft, R[ft], U[y], y, x, y);
			R[L[sz]] = sz; L[R[sz]] = sz;
			U[D[sz]] = sz; D[U[sz]] = sz;
		}
		++cnt[y];	++sz;
	}

	//精确覆盖
	void remove(int now)
	{
		R[L[now]] = R[now];
		L[R[now]] = L[now];
		F(i,D,now) F(j,R,i)
		{
			D[U[j]] = D[j];
			U[D[j]] = U[j];
			--cnt[col[j]];
		}
	}
	void resume(int now)
	{
		F(i,U,now)	F(j,L,i)
		{
			D[U[j]] = j;
			U[D[j]] = j;
			++cnt[col[j]];
		}
		R[L[now]] = now;
		L[R[now]] = now;
	}
	int dfs(int x)
	{
		if (!R[0]) return 1;
		int now = R[0];
		F(i,R,0) if (cnt[now]>cnt[i]) now = i;
		remove(now);
		F(i,D,now)
		{
			ans[x] = row[i];
			F(j,R,i) remove(col[j]);
			if (dfs(x + 1)) return 1;
			F(j,L,i) resume(col[j]);
		}
		resume(now);
		return 0;
	}

	//精确覆盖
	//重复覆盖
	void Remove(int now)
	{
		F(i,D,now) 
		{
			L[R[i]]=L[i];
			R[L[i]]=R[i];
		}
	}
	void Resume(int now)
	{
		F(i,U,now) L[R[i]]=R[L[i]]=i;
	}
	int vis[maxn];
	int flag[maxn];
	int A()
	{
		int dis=0;
		F(i,R,0) vis[i]=0;
		F(i,R,0) if (!vis[i])
		{
			dis++;	vis[i]=1;
			F(j,D,i) F(k,R,j) vis[col[k]]=1;
		}
		return dis;
	}
	void Dfs(int x)
	{
		if (!R[0]) num=min(num,x);
		else if (x+A()<num)
		{
			int now=R[0];
			F(i,R,0) if (cnt[now]>cnt[i]) now = i;
			F(i,D,now)
			{
				Remove(i);F(j,R,i) Remove(j);
				Dfs(x+1);
				F(j,L,i) Resume(j);Resume(i);
			}
		}
	}
	void mul()
	{
		num=0x7FFFFFFF;
	}
	//重复覆盖
}dlx;

void make(int n)
{
	for (int k=1;k<=n;k++)
		for (int i=0;i+k<=n;i++)
			for (int j=0;j+k<=n;j++)
				b[++tot2]=point(i,j,k);
}

void Make(int n)
{
	for (int i=0;i<=n;i++)
	{
		for (int j=0;j<n;j++) a[++tot1]=point(i,j,0);
		if (i==n) continue;
 		for (int j=0;j<=n;j++) a[++tot1]=point(i,j,1);
	}
}

bool get(point &a,point&b)
{
	if (a.x==b.x&&a.z==0&&a.y<b.y+b.z&&a.y>=b.y) return true;
	if (a.y==b.y&&a.z==1&&a.x<b.x+b.z&&a.x>=b.x) return true;
	if (a.x==b.x+b.z&&a.z==0&&a.y<b.y+b.z&&a.y>=b.y) return true;
	if (a.y==b.y+b.z&&a.z==1&&a.x<b.x+b.z&&a.x>=b.x) return true;
	return false;
}

void reset()
{	
	memset(vis,0,sizeof(vis));
	dlx.mul();
	tot1=tot2=0;	
	make(n);	Make(n);
	dlx.reset(tot1,tot2);
	while (m--) 
	{
		scanf("%d",&x);
		for (int i=1;i<=tot2;++i)
			if (!vis[i]&&get(a[x],b[i])) 
			{
				dlx.R[dlx.L[i]]=dlx.R[i];
				dlx.L[dlx.R[i]]=dlx.L[i];
				vis[i]=1;
			}
	}
	for (int i=1;i<=tot1;++i)
		for (int j=1;j<=tot2;++j)
			if (!vis[j]&&get(a[i],b[j])) dlx.insert(i,j);
}

int main()
{
	read(T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		reset();
		dlx.Dfs(0);
		printf("%d\n",dlx.num);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值