C++实现精确覆盖(dlx)。

参考自https://www.cnblogs.com/Rhein-E/p/8969773.html

#include <iostream>
using namespace std;
#define N 1010
typedef struct node
{
	int data;
	struct node *left;
	struct node *right;
	struct node *up;
	struct node *down;
	struct node *column;
}NODE;
NODE *head, *col[N], *last_row[N];
int m, n;
bool dlx();
void remove(NODE*);
void recover(NODE*);

int main()
{
	int a;
	cin >> n >> m;
	head = new NODE;
	for (int i = 1; i <= m; i++)
	{
		col[i] = new NODE;
	}
	col[1]->left = head;
	col[1]->right = col[2];
	col[1]->up = col[1]->down = col[1];
	col[1]->data = 1; col[1]->column = col[1];
	for (int i = 2; i <= m-1; i++)
	{
		col[i]->data = i;
		col[i]->left = col[i - 1];
		col[i]->right = col[i + 1];
		col[i]->up = col[i]->down = col[i];
		col[i]->column = col[i];
	}
	col[m]->data = m;
	col[m]->left = col[m - 1];
	col[m]->right = head;
	col[m]->up = col[m]->down = col[m];
	col[m]->column = col[m];
	head->right = col[1];
	head->left = col[m];
	for (int i = 1; i <= n; i++)
	{
		NODE *last = NULL;
		for (int j = 1; j <= m; j++)
		{
			cin >> a;
			if (a == 1)
			{
				NODE *s = new NODE;
				s->data = i;
				s->column = col[j];
				if (last_row[j] != NULL)
				{
					s->up = last_row[j];
					s->down = last_row[j]->down;
					s->up->down = s;
					col[j]->up = s;
				}
				else
				{
					s->up = col[j];
					s->down = col[j];
					col[j]->up = s;
					col[j]->down = s;
				}
				if (last != NULL)
				{
					s->left = last;
					s->right = last->right;
					s->left->right = s;
				}
				else
				{
					s->left = s->right = s;
				}
				last = s;
				last_row[j] = s;
			}
		}
	}
	if (dlx())
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}

	system("pause");
	return 0;
}

bool dlx()
{
	NODE *c = head->right;
	if (head == head->right)
		return true;
	if (c->down == c)
		return false;
	remove(c);

	for (NODE *i = c->down; i != c; i = i->down)
	{
		for (NODE *j = i->right; j != i; j = j->right)
		{
			remove(j->column);
		}
		if (dlx())
		{
			cout << i->data << endl;
			return true;
		}
		else
		{
			for (NODE *j = i->right; j != i; j = j->right)
			{
				recover(j->column);
			}
		}
	}
	return false;
}

void remove(NODE *c)
{
	c->left->right = c->right;
	c->right->left = c->left;
	if (c->down == c)
		return;
	for (NODE *i = c->down; i != c; i = i->down)
	{
		for (NODE *j = i->right; j != i; j = j->right)
		{
			j->up->down = j->down;
			j->down->up = j->up;
		}
	}
	return;
}

void recover(NODE *c)
{
	c->left->right = c;
	c->right->left = c;
	for (NODE *i = c->down; i != c; i = i->down)
	{
		for (NODE *j = i->right; j != i; j = j->right)
		{
			j->up->down = j;
			j->down->up = j;
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值