poj-3740 Easy Finding

題意:

給出一個n*m的01矩陣,選擇其中的一些行,來精確覆蓋每一列;

只需要輸出是否存在解即可;

n<=16,m<=300;


題解:

DLX裸題,利用雙向十字鏈表優化搜索中的回溯問題;

因爲每一列上都只能有且僅有一個1,所以如果某一列上已經有了1,那麼這一列上有1的其他行也可以被刪除;

根據這個思想是我們有了一個很厲害的剪枝條件,但是如果直接在矩陣中刪除速度太慢,要求空間太多;

所以就有了這種支持快速刪除與恢復的數據結構——雙向鏈表的優化;

具體實現上,我覺得還是指針比較靠譜,因爲所以指針都是循環的所以不存在訪問NULL的問題;


代碼:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 20
#define M 310
using namespace std;
struct node
{
	node *l,*r,*u,*d;
	int belong;
}buf[N*M],*head,*c[M];
int tot,cnt[M];
bool map[N][M];
node *newnode()
{
	return &buf[++tot];
}
void Build(int n,int m)
{
	tot=0;
	head=newnode();
	for(int i=1;i<=m;i++)
		c[i]=newnode();
	for(int i=1;i<=m;i++)
	{
		c[i]->l=c[i-1],c[i]->r=c[i+1];
		c[i]->u=c[i],c[i]->d=c[i];
	}
	c[1]->l=head,c[m]->r=head;
	head->r=c[1],head->l=c[m];
	for(int i=1;i<=n;i++)
	{
		node *t=NULL;
		for(int j=1;j<=m;j++)
		{
			if(map[i][j])
			{
				node *now=newnode();
				if(t==NULL)
					now->l=now->r=now;
				else
					now->l=t,now->r=t->r,
					t->r=now,now->r->l=now;
				now->belong=j;
				now->u=c[j]->u;
				now->d=c[j];
				c[j]->u->d=now;
				c[j]->u=now;
				t=now;
			}
		}
	}
}
void remove(node *x)
{
	x->l->r=x->r;
	x->r->l=x->l;
	for(node *t=x->d;t!=x;t=t->d)
	{
		for(node *k=t->r;k!=t;k=k->r)
		{
			k->u->d=k->d;
			k->d->u=k->u;
		}
	}
}
void resume(node *x)
{
	x->l->r=x;
	x->r->l=x;
	for(node *t=x->d;t!=x;t=t->d)
	{
		for(node *k=t->r;k!=t;k=k->r)
		{
			k->u->d=k;
			k->d->u=k;
		}
	}
}
bool dfs()
{
	node *t=head->r;
	if(t==head)	return 1;
	for(node *i=t->d;i!=t;i=i->d)
	{
		remove(t);
		for(node *j=i->r;j!=i;j=j->r)
		{
			remove(c[j->belong]);
		}
		if(dfs())
			return 1;
		for(node *j=i->r;j!=i;j=j->r)
		{
			resume(c[j->belong]);
		}
		resume(t);
	}
	return 0;
}
int main()
{
	int n,m,i,j,k;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		Build(n,m);
		if(dfs())
			puts("Yes, I found it");
		else
			puts("It is impossible");
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章