DLX(精确覆盖) 16*16数独 POJ 3076 Sudoku

这篇博客详细介绍了如何使用DLX( Dancing Links )算法解决POJ 3076 Sudoku这个16*16数独问题。作者在文中提及遇到的困扰,即C++编译器出现错误而G++通过的情况,问题在于需要多输出一个空行。博客内容包含具体的算法实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sudoku
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 4191 Accepted: 2042

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

 

 

不知为何C++ WA了, G++过了。。。要多输出一个空行呀

代码:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace  std;
const int N = 4 * 16 * 16 * 16 * 16 * 16+10;
const int maxc = 4 * 16 * 16;
#define For(i , s , A) for(int i=A[s];i!=s;i=A[i]) 
int U[N], D[N], L[N], R[N];
int S[maxc], cnt;
int row[N], col[N];
inline int code(int a, int b, int c)
{
	return a * 256 + b * 16 + c;
}

inline void decode(int x, int&a, int&b, int&c)
{
	c = x % 16; x /= 16;
	b = x % 16; x /= 16;
	a = x;
}


int M[5];

void add_row(int x)
{
	int q = cnt;
	for (int i = 0; i < 4; ++i)
	{
		int c = M[i];
		L[cnt] = cnt - 1; R[cnt] = cnt + 1;
		U[cnt] = U[c]; D[cnt] = c;
		D[U[cnt]] = cnt; U[c] = cnt;
		row[cnt] = x; col[cnt] = c;
		++S[c];
		++cnt;
	}
	L[q] = cnt - 1; R[cnt - 1] = q;
}

inline void lrRemove(int x) { L[R[x]] = L[x]; R[L[x]] = R[x]; }
inline void lrResume(int x) { L[R[x]] = R[L[x]] = x; }
inline void udRemove(int x) { U[D[x]] = U[x]; D[U[x]] = D[x]; }
inline void udResume(int x) { U[D[x]] = D[U[x]] = x; }

void RemoveColumn(int c)
{
	lrRemove(c);
	For(i, c, D) {
		For(j, i, R) {
			--S[col[j]];
			udRemove(j);
		}
	}
}

void ResumeColumn(int c)
{
	For(i, c, U) {
		For(j, i, L) {
			++S[col[j]];
			udResume(j);
		}
	}
	lrResume(c);
}

char sudoku[20][20];

void build()
{
	cnt = 0;
	memset(S, 0, sizeof(S));
	for (int i = 0; i <= maxc; ++i)
	{
		L[i] = i - 1; R[i] = i + 1;
		col[i] = i;
		U[i] = D[i] = i;
		++cnt;
	}
	L[0] = cnt - 1; R[cnt - 1] = 0;

	for (int r = 0; r < 16;++r)
	for (int c = 0; c < 16;++c)
	for (int v = 0; v < 16; ++v)
	{
		int x = code(0, r, v); M[0] = x + 1;
		x = code(1, c, v); M[1] = x + 1;
		x = code(2, r, c); M[2] = x + 1;
		x = code(3, r / 4 * 4 + c / 4, v); M[3] = x + 1;
		add_row(code(r, c, v));
	}

	for (int r = 0; r < 16;++r)
	for (int c = 0; c < 16; ++c) if (sudoku[r][c] != '-')
	{
		int v = sudoku[r][c] - 'A';
		int x = code(0, r, v); RemoveColumn(col[x + 1]);
		x = code(1, c, v); RemoveColumn(col[x + 1]);
		x = code(2, r, c); RemoveColumn(col[x + 1]);
		x = code(3, r / 4 * 4 + c / 4, v); RemoveColumn(col[x + 1]);
	}
}

int way[16 * 16 + 10], top;

bool dfs()
{
	if (R[0] == 0) return true;
	int c = L[0];
	For(i, 0, L)
	if (S[i] < S[c]) c = i;

	if (S[c] == 0) return false;
	RemoveColumn(c);
	For(i, c, D) {
		For(j, i, R) RemoveColumn(col[j]);
		way[top++] = row[i];
		if (dfs()) return true;
		--top; 
		For(j, i, L) ResumeColumn(col[j]);
	}
	ResumeColumn(c);
	return false;
}


void solve()
{
	top = 0;
	dfs();
	for (int i = 0; i < top; ++i) {
		int r, c, v;
		decode(way[i], r, c, v);
		sudoku[r][c] = v + 'A';
	}
	for (int i = 0; i < 16; ++i) printf("%s\n", sudoku[i]);
	puts("");
}

int main()
{
	while (scanf("%s", sudoku[0]) == 1)
	{
		for (int i = 1; i < 16; ++i) scanf("%s", sudoku[i]);
		build();
		solve();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值