码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3076 Sudoku DLX精确覆盖

时间:2014-10-14 00:45:47      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   


DLX精确覆盖模版题.....


Sudoku
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 4416   Accepted: 2143

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. 
bubuko.com,布布扣 
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

[Submit]   [Go Back]   [Status]   [Discuss]





#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N=16;
const int maxn=N*N*N+10;
const int maxm=N*N*4+10;
const int maxnode=maxn*4+maxm+10;

char sudoku[maxn];

struct DLX
{
	int n,m,size;
	int U[maxnode],D[maxnode],L[maxnode],R[maxnode],Row[maxnode],Col[maxnode];
	int H[maxnode],S[maxnode];
	int ansd,ans[maxn];
	void init(int _n,int _m)
	{
		n=_n; m=_m;
		for(int i=0;i<=m;i++)
		{
			S[i]=0;
			U[i]=D[i]=i;
			L[i]=i-1;
			R[i]=i+1;
		}
		R[m]=0; L[0]=m;
		size=m;
		for(int i=1;i<=n;i++) H[i]=-1;
	}
	void Link(int r,int c)
	{
		++S[Col[++size]=c];
		Row[size]=r;
		D[size]=D[c];
		U[D[c]]=size;
		U[size]=c;
		D[c]=size;
		if(H[r]<0) H[r]=L[size]=R[size]=size;
		else
		{
			R[size]=R[H[r]];
			L[R[H[r]]]=size;
			L[size]=H[r];
			R[H[r]]=size;
		}
	}
	void remove(int c)
	{
		L[R[c]]=L[c]; R[L[c]]=R[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]];
			}
	}
	void resume(int c)
	{
		for(int i=U[c];i!=c;i=U[i])
			for(int j=L[i];j!=i;j=L[j])
				++S[Col[U[D[j]]=D[U[j]]=j]];
		L[R[c]]=R[L[c]]=c;
	}
	bool Dance(int d)
	{
		if(R[0]==0)
		{
			for(int i=0;i<d;i++) sudoku[(ans[i]-1)/16]=(ans[i]-1)%16+'A';
			//printf("%s\n",sudoku);
			for(int i=0,sz=N*N;i<sz;i++)
            {
                putchar(sudoku[i]);
                if((i+1)%16==0) putchar(10);
            }
			return true;
		}
		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[d]=Row[i];
			for(int j=R[i];j!=i;j=R[j]) remove(Col[j]);
			if(Dance(d+1)) return true;
			for(int j=L[i];j!=i;j=L[j]) resume(Col[j]);
		}
		resume(c);
		return false;
	}
};

DLX dlx;

void place(int& r,int& c1,int& c2,int& c3,int& c4,int i,int j,int k)
{
	r=(i*N+j)*N+k;
	c1=i*N+j+1;
	c2=N*N+N*i+k;
	c3=N*N*2+N*j+k;
	c4=N*N*3+((i/4)*4+(j/4))*N+k;
}

int main()
{
	while(scanf("%s",sudoku)!=EOF)
	{
		for(int i=1;i<16;i++)
            scanf("%s",sudoku+i*16);
		dlx.init(N*N*N,N*N*4);
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++)
			{
				for(int k=1;k<=N;k++)
				{
					if(sudoku[i*N+j]=='-'||sudoku[i*N+j]==k+'A'-1)
					{
						int r,c1,c2,c3,c4;
						place(r,c1,c2,c3,c4,i,j,k);
						dlx.Link(r,c1);
						dlx.Link(r,c2);
						dlx.Link(r,c3);
						dlx.Link(r,c4);
					}
				}
			}
		}
		dlx.Dance(0);
		putchar(10);
	}
	return 0;
}



POJ 3076 Sudoku DLX精确覆盖

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://blog.csdn.net/ck_boss/article/details/40056621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!