BZOJ 1501 NOI2005 智慧珠游戏 Dancing-Links(DLX)

25 篇文章 0 订阅
3 篇文章 0 订阅

题目大意:给定一个10*10的三角形棋盘和12种零件,每种零件只能放一次,可以旋转和翻转,一些零件已经放在了上面,求一种方案,使12个零件无重叠地放在棋盘上

首先这题目一看就是DLX 但是建图真心恶心 需要枚举每一个零件的最多八个朝向的所有位置 我一开始想要全部代码处理 但是后来发现真做不了

于是我选择了打表录入12个零件的所有60种朝向,选择第一排最左面的点作为基点,依次得出每个点关于基点的相对位置,然后再图上枚举基点的位置,若所有点都在图上就加行

一共60*5*2的表 打了40分钟 键盘被敲得噼里啪啦响 晚上还没吃饭 差点把体内(C6H12O6)n全部消耗掉0.0

这个写完之后稍微一调就过去了~ 还WA了一次,忘了输出No solution。。。。。

代码一共6.8KB 表占了很大空间 不过对比一下大牛们的代码发现我的都短了 这年这三道题真是一道比一道恶心

此外这题一共有32288种放法。。。我好无聊。。。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int top;
struct abcd *stack[40000];
struct abcd{
	abcd *l,*r,*u,*d;
	int x,y,num;
	abcd *bottom;
	abcd(abcd *L,abcd *R,abcd *U,abcd *D,int X,int Y,int Num);
	void del();
	void output();
	void restore();
}*head,*heads[100];

const int start[]={0,0,4,6,14,15,19,27,31,39,47,48,52,60};
const int size[]={0,3,4,4,4,5,5,5,5,5,5,5,5};
const int table[60][5][2]={
	
	{ {0,0} , {1,0} , {1,1} , {0,0} , {0,0} },
	{ {0,0} , {1,-1} , {1,0} , {0,0} , {0,0} },
	{ {0,0} , {0,1} , {1,0} , {0,0} , {0,0} },
	{ {0,0} , {0,1} , {1,1} , {0,0} , {0,0} },
	
	{ {0,0} , {0,1} , {0,2} , {0,3} , {0,0} },
	{ {0,0} , {1,0} , {2,0} , {3,0} , {0,0} },
	
	{ {0,0} , {0,1} , {0,2} , {1,0} , {0,0} },
	{ {0,0} , {0,1} , {1,1} , {2,1} , {0,0} },
	{ {0,0} , {1,-2} , {1,-1} , {1,0} , {0,0} },
	{ {0,0} , {1,0} , {2,0} , {2,1} , {0,0} },
	{ {0,0} , {0,1} , {1,0} , {2,0} , {0,0} },
	{ {0,0} , {0,1} , {0,2} , {1,2} , {0,0} },
	{ {0,0} , {1,0} , {2,0} , {2,-1} , {0,0} },
	{ {0,0} , {1,0} , {1,1} , {1,2} , {0,0} },
	
	{ {0,0} , {0,1} , {1,0} , {1,1} , {0,0} },
	
	{ {0,0} , {1,0} , {2,0} , {2,1} , {2,2} },
	{ {0,0} , {0,1} , {0,2} , {1,0} , {2,0} },
	{ {0,0} , {0,1} , {0,2} , {1,2} , {2,2} },
	{ {0,0} , {1,0} , {2,-2} , {2,-1} , {2,0} },
	
	{ {0,0} , {1,-2} , {1,-1} , {1,0} , {1,1} },
	{ {0,0} , {1,0} , {2,0} , {2,1} , {3,0} },
	{ {0,0} , {0,1} , {0,2} , {0,3} , {1,1} },
	{ {0,0} , {1,-1} , {1,0} , {2,0} , {3,0} },
	{ {0,0} , {1,0} , {1,1} , {2,0} , {3,0} },
	{ {0,0} , {0,1} , {0,2} , {0,3} , {1,2} },
	{ {0,0} , {1,0} , {2,-1} , {2,0} , {3,0} },
	{ {0,0} , {1,-1} , {1,0} , {1,1} , {1,2} },
	
	{ {0,0} , {0,2} , {1,0} , {1,1} , {1,2} },
	{ {0,0} , {0,1} , {1,0} , {2,0} , {2,1} },
	{ {0,0} , {0,1} , {0,2} , {1,0} , {1,2} },
	{ {0,0} , {0,1} , {1,1} , {2,0} , {2,1} },
	
	{ {0,0} , {0,1} , {1,0} , {1,1} , {2,1} },
	{ {0,0} , {0,1} , {1,-1} , {1,0} , {1,1} },
	{ {0,0} , {1,0} , {1,1} , {2,0} , {2,1} },
	{ {0,0} , {0,1} , {0,2} , {1,0} , {1,1} },
	{ {0,0} , {0,1} , {0,2} , {1,1} , {1,2} },
	{ {0,0} , {1,-1} , {1,0} , {2,-1} , {2,0} },
	{ {0,0} , {0,1} , {1,0} , {1,1} , {1,2} },
	{ {0,0} , {0,1} , {1,0} , {1,1} , {2,0} },
	
	{ {0,0} , {1,-1} , {1,0} , {2,-1} , {3,-1} },
	{ {0,0} , {0,1} , {0,2} , {1,2} , {1,3} },
	{ {0,0} , {1,0} , {2,-1} , {2,0} , {3,-1} },
	{ {0,0} , {0,1} , {1,1} , {1,2} , {1,3} },
	{ {0,0} , {0,1} , {1,-2} , {1,-1} , {1,0} },
	{ {0,0} , {1,0} , {2,0} , {2,1} , {3,1} },
	{ {0,0} , {0,1} , {0,2} , {1,-1} , {1,0} },
	{ {0,0} , {1,0} , {1,1} , {2,1} , {3,1} },
	
	{ {0,0} , {1,-1} , {1,0} , {1,1} , {2,0} },
	
	{ {0,0} , {1,0} , {1,1} , {2,1} , {2,2} },
	{ {0,0} , {0,1} , {1,-1} , {1,0} , {2,-1} },
	{ {0,0} , {0,1} , {1,1} , {1,2} , {2,2} },
	{ {0,0} , {1,-1} , {1,0} , {2,-2} , {2,-1} },
	
	{ {0,0} , {1,-3} , {1,-2} , {1,-1} , {1,0} },
	{ {0,0} , {1,0} , {2,0} , {3,0} , {3,1} },
	{ {0,0} , {0,1} , {0,2} , {0,3} , {1,0} },
	{ {0,0} , {0,1} , {1,1} , {2,1} , {3,1} },
	{ {0,0} , {0,1} , {1,0} , {2,0} , {3,0} },
	{ {0,0} , {0,1} , {0,2} , {0,3} , {1,3} },
	{ {0,0} , {1,0} , {2,0} , {3,-1} , {3,0} },
	{ {0,0} , {1,0} , {1,1} , {1,2} , {1,3} }
};
int map[11][11],repos[11][11],cnt=12;
bool appeared[13],mark[11][11];

void DLX();
void output();
void find(int p);
inline char getnum();
void del_row(abcd *pos);
void add(int p,int pos);
void del_column(abcd *pos);

int main()
{
	int i,j;
	abcd *last;
	head=new abcd(0x0,0x0,0x0,0x0,0,0,0);
	for(last=head,i=1;i<=67;i++)
		last=new abcd(last,0x0,0x0,0x0,0,0,0),heads[i]=last;
	
	for(i=1;i<=10;i++)
		for(j=1;j<=i;j++)
			repos[i][j]=++cnt,map[i][j]=getnum();
	
	for(i=1;i<=12;i++)
		if(!appeared[i])
			for(j=start[i];j!=start[i+1];j++)
				add(i,j);
		else
			find(i);
	DLX();
	puts("No solution");
	return 0;
}

abcd :: abcd(abcd *L,abcd *R,abcd *U,abcd *D,int X,int Y,int Num)
{
	l=L;if(L)L->r=this;
	r=R;if(R)R->l=this;
	u=U;if(U)U->d=this;
	d=D;if(D)D->u=this;
	x=X;y=Y;num=Num;
	bottom=d;
	if(bottom)
		bottom->x++;
}
void abcd :: del()
{
	if(l)l->r=r;
	if(r)r->l=l;
	if(u)u->d=d;
	if(d)d->u=u;
	if(bottom)
		bottom->x--;
	stack[++top]=this;
}
void abcd :: restore()
{
	if(l)l->r=this;
	if(r)r->l=this;
	if(u)u->d=this;
	if(d)d->u=this;
	if(bottom)
		bottom->x++;
}
void output()
{
	int i,j;
	for(i=1;i<=10;i++)
	{
		for(j=1;j<=i;j++)
			putchar(map[i][j]+'A'-1);
		putchar('\n');
	}
}
void del_column(abcd *pos)
{
	abcd *temp1,*temp2;
	for(temp1=pos->u;temp1;temp1=temp1->u)
	{
		for(temp2=temp1->l;temp2;temp2=temp2->l)
			temp2->del();
		for(temp2=temp1->r;temp2;temp2=temp2->r)
			temp2->del();
		temp1->del();
	}
	pos->del();
}
void del_row(abcd *pos)
{
	if(!pos)
		return ;
	del_row(pos->r);
	del_column(pos->bottom);
	int x=pos->x,y=pos->y,num=pos->num;
		map[x][y]=num;
}
void DLX()
{
	if(!head->r)
	{
		output();
		exit(0);
	}
	int bottom=top,minnum=0x7fffffff;
	abcd *temp,*mintemp;
	for(temp=head->r;temp;temp=temp->r)
		if(temp->x<minnum)
			minnum=temp->x,mintemp=temp;
	for(temp=mintemp->u;temp;temp=temp->u)
	{
	
		for(mintemp=temp;mintemp->l;mintemp=mintemp->l);
		del_row(mintemp);
		
		DLX();
		while(top!=bottom)
			stack[top--]->restore();
	}
}
inline char getnum()
{
	char c;
	do c=getchar(); while(c==' '||c=='\n'||c=='\r');
	if(c=='.')
		return 0;
	appeared[c-'A'+1]=1;
	return c-'A'+1;
}
void add(int p,int pos)
{
	int i,j,k,x,y;
	for(i=1;i<=10;i++)
		for(j=1;j<=i;j++)
		{
			for(k=0;k<size[p];k++)
			{
				x=i+table[pos][k][0];
				y=j+table[pos][k][1];
				if(x>0&&y>0&&x<=10&&y<=x);
				else
					break;
			}
			if(k==size[p])
			{
				abcd *last,*temp;
				temp=heads[ p ];
				last = new abcd(0x0,0x0,temp->u,temp,0,0,0);
				for(k=0;k<size[p];k++)
				{
					x=i+table[pos][k][0];
					y=j+table[pos][k][1];
					temp=heads[ repos[x][y] ];
					last = new abcd(last,0x0,temp->u,temp,x,y,p);
				}
			}
		}
}
void find(int p)
{
	int i,j;
	abcd *last,*temp;
	temp=heads[ p ];
	last = new abcd(0x0,0x0,temp->u,temp,0,0,0);
	for(i=1;i<=10;i++)
		for(j=1;j<=i;j++)
			if(map[i][j]==p)
				temp=heads[ repos[i][j] ],last = new abcd(last,0x0,temp->u,temp,i,j,p);
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值