poj 3074 Sudoku dlx解数独

分析:

dlx是从数据结构角度优化01矩阵精确覆盖和重复覆盖的数据结构,它用十字链表只存贮矩阵中的非0元,而01矩阵精确覆盖dfs过程中矩阵会越来越稀疏而且每次恢复现场会浪费大量时间,dlx恰好能解决这两个问题。本题关键是将数独问题转化为01矩阵精确覆盖。数独转化为精确覆盖问题的方法还是参照Knuth的论文,如果读取到一个格子是空的,那么加9行,分别表示这个格子填1到9这9个数字,如果读取到的格子是一个数字,那么就加一行就可以了,然后列有9*9*4列,前81列表示这一行表示填的是第i行第j列的格子,接下来81列表示第i行填写k,接下来81列表示第j列填写k,最后81列表示对应九宫格填写k。

//poj 3074
//sep9
#include <cstdio>
#include <cstdlib>
#define INT_MAX  2147483647
using namespace std;
const int MAX=1024;
const int col_num=9*9*4;
const int head=0;
const int delta[]={1,82,163,244};
int cnt[MAX],st[MAX];
int left[MAX*MAX],right[MAX*MAX],up[MAX*MAX],down[MAX*MAX];
int row[MAX*MAX],col[MAX*MAX];

int K,M;//k:node's idx  M:row's number

struct ANS
{
	int r,c,k;
}ans[MAX*MAX];

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

int make_col_head(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(int ids,int c)
{
	++K;
	++cnt[c];
	col[K]=c;
	row[K]=M;

	left[K]=ids;
	right[K]=right[ids];
	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(int i,int j,int k)
{
	++M;
	ans[M].r=i;
	ans[M].c=j;
	ans[M].k=k+1;
	int ids=make_col_head(9*i+j+delta[0]);
	addcol(ids,9*i+k+delta[1]);
	addcol(ids,9*j+k+delta[2]);
	addcol(ids,9*(i/3*3+j/3)+k+delta[3]);
}

void remove(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(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(int k)
{
	if(right[head]==head){
		char s[128];
		for(int i=0;i<k;++i)
			s[ans[st[i]].r*9+ans[st[i]].c]=ans[st[i]].k+'0';
		s[81]='\0';
		puts(s);
		return true;
	}
	int s=INT_MAX,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;
} 

int main()
{
	char s[128];
	while(scanf("%s",s)==1&&s[0]!='e'){
		init();
		for(int i=0;i<9;++i)
			for(int j=0;j<9;++j)
				if(s[i*9+j]=='.'){
					for(int k=0;k<9;++k)
						addrow(i,j,k);
				}
				else
					addrow(i,j,s[i*9+j]-'1');
		dfs(0);
	}
	return 0;
}
时间: 2024-10-07 21:22:25

poj 3074 Sudoku dlx解数独的相关文章

poj 3076 Sudoku dlx解数独

16*16的数独,类似poj 3074. //poj 3076 //sep9 #include <cstdio> #include <cstdlib> #define INT_MAX 2147483647 using namespace std; const int col_num=16*16*4; const int row_num=16*16*16+10; const int head=0; const int MAX=row_num*4+col_num+10; const i

POJ 3074 Sudoku (DLX)

Sudoku Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3074 Appoint description:  System Crawler  (2015-04-18) Description In the game of Sudoku, you are given a large 9 × 9 grid divided into sm

(简单) POJ 3074 Sudoku, DLX+精确覆盖。

Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . . . 6 7 3 5 . . . . . . . 2 9 3 . 5 6 9 2 . 8 . . . . . . . . . . . 6 . 1 7 4 5 . 3 6 4 . . . . . . . 9 5 1

POJ 3074 Sudoku DLX精确覆盖

DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: 2945 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . .

poj 3074 Sudoku(Dancing Links)

Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8152   Accepted: 2862 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . . . 6 7 3 5 . . .

[LeetCode] Sudoku Solver 解数独,递归,回溯

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. Hide Tags B

(简单) POJ 3076 Sudoku , DLX+精确覆盖。

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 g

POJ 3047 Sudoku DLX精确覆盖

DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: 2945 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . .

POJ 3076 Sudoku DLX精确覆盖

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 fi