POJ-3076 Sudoku(Dancing Link 算法)

Sudoku
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 4835 Accepted: 2298

Description

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.

Write a Sudoku playing program that reads data sets from a text file.

Input

Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i-th string stands for the i-th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,…,P,-}, where – (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file.

Output

The program prints the solution of the input encoded grids in the same format and order as used for input.

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

Source

Southeastern Europe 2006

裸的dlx

#define N 16*16*16*4+16*16*4+233
#include <string>
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
struct Queue
{
	int l;
	int r;
	int up;
	int down;
	int ans;	
}; 
Queue f[N];
char s[17][17];
int tot=0,TOT=0,H[17][17],L[17][17],In[17][17],Num[17][17],C[N],Cn[N],ans[17][17],Ans[16*16+1];
void LRlink(int x,int y)
{ 
	f[x].r=y;
	f[y].l=x;
}
void UPlink(int x,int y)
{
	f[x].down=y;
	f[y].up=x;
}
int find(int x,int y)
{
	x/=4;
	y/=4;
	return(x*4+y);
}
void Linkline()
{
	LRlink(tot-3,tot-2);
	LRlink(tot-2,tot-1);
	LRlink(tot-1,tot);
	LRlink(tot,tot-3);
} 
void remove(int x)
{
	f[f[x].l].r=f[x].r;
	f[f[x].r].l=f[x].l;
	for(int i=f[x].down;i!=x;i=f[i].down)
	 for(int j=f[i].r;j!=i;j=f[j].r)
	  {
	  	Cn[C[j]]--;
	  	f[f[j].up].down=f[j].down;
	  	f[f[j].down].up=f[j].up;
	  }
} 
void resume(int x)
{
	f[f[x].l].r=x;
	f[f[x].r].l=x;
	for(int i=f[x].down;i!=x;i=f[i].down)
	 for(int j=f[i].l;j!=i;j=f[j].l)
	  {
	  	Cn[C[j]]++;
	  	f[f[j].up].down=j;
	  	f[f[j].down].up=j;
	  }
}
void print()
{
	for(int i=0;i<16;i++)
      printf("%s\n",s[i]);
 } 
bool dance(int k)
{
	int c=f[0].r;
	if(c==0)
	{
		print();
		return true;
	}
	int Min=c;
	for(int i=c;i!=0;i=f[i].r)
	 if(Cn[Min]>Cn[i]) Min=i;
	c=Min;
	remove(c);
	for(int i=f[c].down;i!=c;i=f[i].down)
	{
		int x=f[i].ans/(16*16),y=(f[i].ans/16)%16,z=f[i].ans%16;
		s[x][y]='A'+z;	
		for(int j=f[i].r;j!=i;j=f[j].r) remove(C[j]);
		if(dance(k+1)) return true;
 	    for(int j=f[i].l;j!=i;j=f[j].l) resume(C[j]);
	}
	resume(c);
 	return false;
}
bool read() 
{  
    for(int i=0;i<16;i++)  
     if(scanf("%s",s[i])==EOF) return false;  
    return true;  
}  
void UPdate(int &x)
{
	UPlink(x,++tot); 
	C[tot]=C[x];
	Cn[C[x]]++;
	x=tot;
}
void Lianf(int &x)
{ 
	x=++tot;
	C[x]=x;
}
int main()
{
   while(read())
   {
   		tot=0;
   		memset(C,0,sizeof(C));
   		memset(Cn,0,sizeof(Cn));
		for(int i=0;i<16;i++)
		 for(int j=0;j<16;j++)
		 {
		 	Lianf(H[i][j]);
		 	Lianf(L[i][j]);
		 	Lianf(In[i][j]);
		 	Lianf(Num[i][j]);
		 }                           //创建表头 
   	    for(int i=0;i<tot+1;i++) LRlink(i,(i+1)%(tot+1));         //表头首尾对接 
	     for(int i=0;i<16;i++) 
	      for(int j=0;j<16;j++)
		  {
   	   	  	char Char;
		  	Char=s[i][j];
	  	    for(int k=0;k<16;k++)
		  	if(Char=='-' || (Char-'A'==k))
   	  		{
   	   	    	UPdate(H[i][k]);
			    UPdate(L[j][k]);
			   	UPdate(In[find(i,j)][k]);
			   	UPdate(Num[i][j]);
				Linkline();
				for(int num=tot-3;num<=tot;num++) f[num].ans=i*16*16+j*16+k;
			}	 	  
   	      }
   	    for(int i=0;i<16;i++)
		 for(int j=0;j<16;j++)
		 {
		 	UPlink(H[i][j],C[H[i][j]]);
		 	UPlink(L[i][j],C[L[i][j]]);
		 	UPlink(In[i][j],C[In[i][j]]);
		 	UPlink(Num[i][j],C[Num[i][j]]);
		 }    //初始化完毕   
		dance(1);
   	}
 } 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值