题解 UVA1309 【Sudoku】

题目链接:Link

Solution

第一次写DLX,写篇题解纪念一下……o(* ̄▽ ̄*)o


这题是一个典型的精确覆盖问题,每一行、每一列、每一个小方格都要有对应的数字,因此可以直接套模型,详细解释见代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<cassert>
using namespace std;
const int maxn=2010;//注意这里的n和数独的大小不是同一个概念!
const int maxnode=20000;//最多有16384个节点
struct DLX
{
	int n,sz;
	int s[maxn];
	int row[maxnode],col[maxnode];
	int L[maxnode],R[maxnode],U[maxnode],D[maxnode];
	int ansd,ans[maxnode];
	void init(int n)//初始化,0为超级节点,1~n为每列的虚拟节点
	{
		this->n=n;
		sz=n+1;
		memset(s,0,sizeof(s));
		
		for(int i=0;i<=n;i++)
			{ U[i]=i; D[i]=i; L[i]=i-1; R[i]=i+1; } 
		R[n]=0;L[0]=n;
	}
	void addNodes(int r,const vector<int> &columns)
	{//在第r行(最后一行)添加这些节点
		int first=sz,c_sz=columns.size();
		for(int i=0;i<c_sz;i++)
		{
			int c=columns[i];
			L[sz]=sz-1; R[sz]=sz+1; D[sz]=c; U[sz]=U[c];
			D[U[c]]=sz; U[c]=sz;
			row[sz]=r; col[sz]=c;
			s[c]++;sz++;
		}
		R[sz-1]=first; L[first]=sz-1;
	}
	
	#define For(i,A,s) for(int i=A[s];i!=s;i=A[i])
	void remove(int c)//覆盖第c个目标
	{
		L[R[c]]=L[c];
		R[L[c]]=R[c];
		For(i,D,c)
			For(j,R,i)//撕开所有相关节点
			{ U[D[j]]=U[j]; D[U[j]]=D[j]; --s[col[j]]; }
	}
	void restore(int c)
	{//复原
		For(i,U,c)
			For(j,L,i)
			{ ++s[col[j]]; U[D[j]]=j; D[U[j]]=j; }
		L[R[c]]=c;
		R[L[c]]=c;
	}
	
	bool dfs(int d)
	{
		if(R[0]==0)
		{
			ansd=d;
			return true;
		}
		
		int c=R[0];
		For(i,R,0) if(s[i]<s[c]) c=i;
		
		remove(c);
		For(i,D,c)
		{
			ans[d]=row[i];
			For(j,R,i) remove(col[j]);//为了确保之覆盖一次
			if(dfs(d+1)) return true;
			For(j,L,i) restore(col[j]);//回溯
		}
		restore(c);//回溯
		
		return false;
	}
	
	bool solve(vector<int> &v)//对外接口
	{
		v.clear();
		if(!dfs(0)) return false;
		for(int i=0;i<ansd;i++) v.push_back(ans[i]);
		return true;
	}
};
DLX solver;
const int Slot=0;
const int Row=1;
const int Col=2;
const int Sub=3;
inline int encode(int a,int b,int c)//统一的编码解码函数
{ return a*256+b*16+c+1; }
inline void decode(int code,int &a,int &b,int &c)
{
	code--;
	c=code%16;code/=16;
	b=code%16;code/=16;
	a=code;
}
char mp[16][20];
bool read()
{
	for(int i=0;i<16;i++)
		if(scanf("%s",mp[i])!=1) return false;
	return true;
}

int main()
{
#ifdef local
	freopen("pro.in","r",stdin);
#endif
	int kase=0;
	while(read())
	{
		if(++kase!=1) printf("\n");
		solver.init(1024);
		for(int r=0;r<16;r++)
			for(int c=0;c<16;c++)
				for(int v=0;v<16;v++)
					if(mp[r][c]=='-'||mp[r][c]=='A'+v)
					{//转换成DLX中的目标
						vector<int> cols;
						cols.push_back(encode(Slot,r,c));
						cols.push_back(encode(Row,r,v));
						cols.push_back(encode(Col,c,v));
						cols.push_back(encode(Sub,(r/4)*4+c/4,v));
						solver.addNodes(encode(r,c,v),cols);
					}
		
		vector<int> ans;
		solver.solve(ans);
		
		for(int i=0;i<ans.size();i++)
		{//读取答案
			int r,c,v;
			decode(ans[i],r,c,v);
			mp[r][c]='A'+v;
		}
		for(int i=0;i<16;i++)
			printf("%s\n",mp[i]);
	}
	return 0;
}
/*
Sample Input

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK 
*/ 
posted @ 2019-08-19 21:53  happyZYM  阅读(154)  评论(0编辑  收藏  举报