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 int delta[]={1,16*16+1,16*16*2+1,16*16*3+1};
int cnt[col_num+10],st[col_num+10];
int left[MAX],right[MAX],up[MAX],down[MAX];
int row[MAX],col[MAX];

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

struct ANS
{
	int r,c,k;
}ans[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;
	int ids=make_col_head(16*i+j+delta[0]);
	addcol(ids,16*i+k+delta[1]);
	addcol(ids,16*j+k+delta[2]);
	addcol(ids,16*(i/4*4+j/4)+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[300];
		for(int i=0;i<k;++i)
			s[ans[st[i]].r*16+ans[st[i]].c]=ans[st[i]].k+'A';
		for(int i=0;i<16;++i){
			for(int j=0;j<16;++j)
				putchar(s[i*16+j]);
			puts("");
		}
		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[20];
	while(scanf("%s",s)==1){
		init();
		for(int j=0;j<16;++j)
			if(s[j]=='-'){
				for(int k=0;k<16;++k)
					addrow(0,j,k);
			}
			else
				addrow(0,j,s[j]-'A');			

		for(int i=1;i<16;++i){
			scanf("%s",s);
			for(int j=0;j<16;++j)
				if(s[j]=='-'){
					for(int k=0;k<16;++k)
						addrow(i,j,k);
				}
				else
					addrow(i,j,s[j]-'A');
		}
		dfs(0);
		puts("");
	}
	return 0;
}
时间: 2024-10-12 21:26:13

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

poj 3074 Sudoku dlx解数独

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

(简单) 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 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

POJ 3076 Sudoku (dancing links)

题目大意: 16*16的数独. 思路分析: 多说无益. 想说的就是dancing links 的行是按照 第一行第一列填 1 第一行第二列填 2 -- 第一行第十五列填15 第一行第二列填 1 -- 第二行.... 列的纺织则是 第一行放1,第一行放2,..第十六行放16...第一列放1..第一列放2...第十六列放16..第一块区域放1 ....然后在最后81位就是放自己的位置,准确的说就是 r*m+c. #include <cstdio> #include <iostream>

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

[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 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 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 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 . .