跳到正文

题解

SP1110 题解

Hagasei约 5 分钟
原文
文章信息与版本

文章信息

发布
2022年2月25日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
1
题目
SP1110

版本

  1. v1当前

简单 dlx。与 P1784 如出一辙。只不过把 3 改成了 4。

相应构造矩阵为:(头上带横线的是十六进制)

第 abc‾\overline{abc} 行表示在数独 (a,b)(a,b) 填 cc

第 0xy‾+1\overline{0xy}+1 列限制每格一个数

第 1xy‾+1\overline{1xy}+1 列限制每行一个 yy

第 2xy‾+1\overline{2xy}+1 列限制每列一个 yy

第 3xy‾+1\overline{3xy}+1 列限制每宫一个 yy

code:

C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN=5000,MAXT=5e6;
int a[20][20];
struct dlx{
	int cnt;
	int l[MAXT],r[MAXT],u[MAXT],d[MAXT];
	int row[MAXT],col[MAXT],h[MAXN],s[MAXN];
	int ans[MAXN];
	void init(int n,int m){
		for(int i=0;i<=m;++i){
			r[i]=i+1,l[i]=i-1;
			u[i]=d[i]=i;
		}
		memset(s,0,sizeof s);
		memset(h,-1,sizeof h);
		r[m]=0,l[0]=m,cnt=m+1;
	}
	inline void addnode(int R,int C){
		s[C]++;
		row[cnt]=R,col[cnt]=C;
		d[cnt]=C;
		u[cnt]=u[C];
		d[u[C]]=cnt;
		u[C]=cnt;
		if(h[R]==-1)h[R]=l[cnt]=r[cnt]=cnt;
		else{
			r[cnt]=h[R];
			l[cnt]=l[h[R]];
			r[l[h[R]]]=cnt;
			l[h[R]]=cnt;
		}
		++cnt;
	}
	inline void remove(int c){
		r[l[c]]=r[c],l[r[c]]=l[c];
		for(int i=d[c];i!=c;i=d[i]){
			for(int j=r[i];j!=i;j=r[j]){
				u[d[j]]=u[j];
				d[u[j]]=d[j];
				s[col[j]]--;
			}
		}
	}
	inline void resume(int c){
		for(int i=u[c];i!=c;i=u[i]){
			for(int j=l[i];j!=i;j=l[j]){
				u[d[j]]=j;
				d[u[j]]=j;
				s[col[j]]++;
			}
		}
		r[l[c]]=l[r[c]]=c;
	}
	bool dance(int depth){
		if(r[0]==0){
			int x,y,z;
			for(int i=0;i<depth;++i){
				x=(ans[i]-1)/16/16;
				y=(ans[i]-1)/16%16;
				z=ans[i]%16;
				if(z==0)z=16;
				a[x][y]=z;
			}
			return 1;
		}
		int c=r[0];
		for(int i=r[0];i!=0;i=r[i])if(s[i]<s[c])c=i;
		remove(c);
		for(int i=d[c];i!=c;i=d[i]){
			ans[depth]=row[i];
			for(int j=r[i];j!=i;j=r[j])remove(col[j]);
			if(dance(depth+1))return 1;
			for(int j=l[i];j!=i;j=l[j])resume(col[j]);
		}
		resume(c);
		return 0;
	}
}D;
bool s[5000][5000];
int main(){
	int T;
	cin>>T;
	while(T--){
		D.init(4096,1024);
		memset(s,0,sizeof s);
		for(int i=0;i<16;++i){
			for(int j=0;j<16;++j){
				char p;
				cin>>p;
				int x=p=='-'?0:p-'A'+1;
				for(int k=1;k<=16;++k){
					if(x!=k&&x!=0)continue;
					int o=i*256+j*16+k;
					s[o][i*16+j+1]=s[o][i*16+256+k]=
					s[o][j*16+512+k]=s[o][(i/4*4+j/4)*16+768+k]=1;
				}
			}
		}
		for(int i=1;i<=4096;++i){
			for(int j=1;j<=1024;++j){
				if(s[i][j]){
					D.addnode(i,j);
				}
			}
		}
		D.dance(0);
		for(int i=0;i<16;++i){
			for(int j=0;j<16;++j){
				putchar(a[i][j]+'A'-1);
			}
			putchar('\n');
		}
		putchar('\n');
	}
}
原文