poj3076(16*16数独)

转载自:http://blog.csdn.net/lyhypacm/article/details/5923287


题意:

一个16*16的数独问题。


思路:

和poj3024一样,就改点东西。


代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int oo=0x3f3f3f3f;
const int nR=16*16*16+10;
const int nC=16*16*4;
const int MAX=nR*4+nC+10;
const int delta[]={1,16*16+1,16*16*2+1,16*16*3+1};
const int head=0;

int cnt[nC+10],st[nC+10];
int left[MAX],right[MAX],up[MAX],down[MAX];
int row[MAX],col[MAX];
struct Ans
{
	int r,c,k;
}ans[MAX];
int M,K;

void remove(const int& c)
{
	left[right[c]]=left[c];
	right[left[c]]=right[c];
	for(int i=down[c];i!=c;i=down[i])
		for(int j=right[i];j!=i;j=right[j])
		{
			up[down[j]]=up[j];
			down[up[j]]=down[j];
			cnt[col[j]]--;
		}
}

void resume(const int& c)
{
	for(int i=up[c];i!=c;i=up[i])
		for(int j=left[i];j!=i;j=left[j])
		{
			down[up[j]]=j;
			up[down[j]]=j;
			cnt[col[j]]++;
		}
	left[right[c]]=c;
	right[left[c]]=c;
}

bool dfs(const int& k)
{
	if(right[head]==head)
	{
		char s[300]={0};
		for(int i=0;i<k;i++)
			s[ans[st[i]].r*16+ans[st[i]].c]=ans[st[i]].k+'A';
		for(int i=0;i<16;i++)
		{
			for(int j=0;j<16;j++)
			{
				putchar(s[i*16+j]);
			}
			puts("");
		}
		return true;
	}
	
	int s=oo,c=0;
	for(int i=right[head];i!=head;i=right[i])
		if(cnt[i]<s)
		{
			s=cnt[i];
			c=i;
		}
	
	remove(c);
	for(int i=down[c];i!=c;i=down[i])
	{
		st[k]=row[i];
		for(int j=right[i];j!=i;j=right[j])
			remove(col[j]);
		if(dfs(k+1))
			return true;
		for(int j=left[i];j!=i;j=left[j])
			resume(col[j]);
	}
	
	resume(c);
	
	return false;
}

void init()
{
	left[head]=nC;
	right[head]=1;
	up[head]=down[head]=head;
	for(int i=1;i<=nC;i++)
	{
		left[i]=i-1;
		right[i]=(i+1)%(nC+1);
		up[i]=down[i]=i;
		cnt[i]=0;
		col[i]=i;
		row[i]=0;
	}
	M=0;
	K=nC;
}

int makecolhead(const int& c)
{
	K++;
	cnt[c]++;
	col[K]=c;
	row[K]=M;
	
	left[K]=right[K]=K;
	
	up[K]=c;
	down[K]=down[c];
	up[down[K]]=K;
	down[up[K]]=K;
	return K;
}

void addcol(const int& id,const int& c)
{
	K++;
	cnt[c]++;
	col[K]=c;
	row[K]=M;
	
	left[K]=id;
	right[K]=right[id];
	left[right[K]]=K;
	right[left[K]]=K;
	
	up[K]=c;
	down[K]=down[c];
	up[down[K]]=K;
	down[up[K]]=K;
}

void addrow(const int& i,const int& j,const int& k)
{
	int id;
	M++;
	ans[M].r=i;
	ans[M].c=j;
	ans[M].k=k;
	id=makecolhead(16*i+j+delta[0]);
	addcol(id,16*i+k+delta[1]);
	addcol(id,16*j+k+delta[2]);
	addcol(id,(i/4*4+j/4)*16+k+delta[3]);
}

int main()
{
	char str[300];
	char s[300];
	int pos;
	bool blocks=false;
	while(~scanf("%s",str))
	{
		if(blocks)
			puts("");
		else
			blocks=true;
		init();
		pos=0;
		for(int i=0;i<16;i++)
			s[pos++]=str[i];
		for(int i=1;i<16;i++)
		{
			scanf("%s",str);
			for(int j=0;j<16;j++)
				s[pos++]=str[j];
		}
		for(int i=0;i<16;i++)
			for(int j=0;j<16;j++)
				if(s[i*16+j]=='-')
					for(int k=0;k<16;k++)
						addrow(i,j,k);
				else
					addrow(i,j,s[i*16+j]-'A');
		
		dfs(0);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值