数独板检查算法 - 除了重复之外还有什么要检查的吗?
Sudoku board check algorithm - is there anything else to check other than duplicates?
我做了一个算法来检查棋盘是否非法,它检查行、列和扇区中的重复项,它对重复项很有效,但在玩了几局之后我得到了以下棋盘:
没有解决方案(没有B5和A7的候选)但没有重复。我们是否总是需要检查是否没有候选人以及检查重复项?还有什么要检查的吗?
只需检查重复项即可让您证明明显的非法填充案例。
恐怕你必须尝试解决数独板才能证明部分填充是合法的,即有解决方案。找到一个解决方案就足够了,但可能还有其他解决方案。找到所有解决方案并不难。
对于您提供的情况,证明没有解决方案很容易,一步就会导致失败(单元格 A7 的所有可能解决方案都会产生重复),但在一般情况下可能会更复杂,因为它需要多个步骤。
一种蛮力方法,例如尝试每个空方块的所有可能性并递归,具有可怕的复杂性。您可以在互联网上找到捷径并用您自己的代码进行研究。
编辑: 如有疑问,请尝试暴力破解!数独在一般情况下可能是 NP-Complete,但在实践中它似乎并没有显着发散。我反思了上面的初步分析,并决定尝试一下蛮力。我写了一个小程序来解决命令行上给出的数独问题。即使在我的旧 MacBook Air 上,它也能在不到几毫秒的时间内解决我尝试过的所有问题。
再次编辑:我用通用版本更新了代码,该版本使用了更好的算法并且快了近 80 倍。
代码如下:
/* Generic Sudoku solver by Charles Gordon */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef GEN
#define GEN 3
#endif
#define SIZE (GEN*GEN)
typedef unsigned char u8_t;
typedef unsigned long long llu_t;
static int print_count = 0, print_solutions = 1;
/* utility arrays to dispatch cell number to signature arrays */
static int rowsig[SIZE * SIZE], colsig[SIZE * SIZE], regsig[SIZE * SIZE];
static void sigcell_init() {
for (int i = 0, row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++, i++) {
rowsig[i] = row;
colsig[i] = SIZE + col;
regsig[i] = SIZE + SIZE + (row / GEN) * GEN + (col / GEN);
}
}
}
static void print_board(const int *board, const char *header) {
printf("%s:\n", header);
for (int i = 0, row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++, i++)
printf("%*.0d", 2 + (SIZE > 9) + (SIZE > 99), board[i]);
putchar('\n');
}
}
static int validate_board(const int *board, u8_t sigs[3 * SIZE][SIZE]) {
memset(sigs, 0, 3 * SIZE * SIZE * sizeof(sigs[0][0]));
for (int i = 0; i < SIZE * SIZE; i++) {
int val = board[i];
if (val == 0)
continue;
if (val < 0 || val > SIZE)
return 2; /* found invalid value */
val -= 1;
if (sigs[rowsig[i]][val] | sigs[colsig[i]][val] | sigs[regsig[i]][val])
return 1; /* found duplicate */
sigs[rowsig[i]][val] = sigs[colsig[i]][val] = sigs[regsig[i]][val] = 1;
}
return 0; /* board is valid */
}
static llu_t try_board(int *board, u8_t sigs[3 * SIZE][SIZE], int *empty, int emp) {
llu_t count, total = 0;
int n, cell;
u8_t *rowsigp, *colsigp, *regsigp;
if (emp == 0) {
if (print_solutions)
print_board(board, "found a board solution");
return 1;
}
cell = *empty; /* next cell to try and populate */
rowsigp = sigs[rowsig[cell]];
colsigp = sigs[colsig[cell]];
regsigp = sigs[regsig[cell]];
for (n = 0; n < SIZE; n++) {
/* check if value is possible */
if ((rowsigp[n] | colsigp[n] | regsigp[n]) == 0) {
rowsigp[n] = colsigp[n] = regsigp[n] = 1;
board[cell] = n + 1;
if ((count = try_board(board, sigs, empty + 1, emp - 1)) > 0) {
total += count;
if (!print_count)
break;
}
rowsigp[n] = colsigp[n] = regsigp[n] = 0;
board[cell] = 0;
}
}
return total;
}
int main(int argc, char *argv[]) {
int board[SIZE * SIZE]; /* board values: empty=0 */
u8_t sigs[3 * SIZE][SIZE]; /* signatures for row, col and regions */
int empty[SIZE * SIZE]; /* list of empty cells */
int i, n;
llu_t count = 0;
sigcell_init();
/* initialize board */
for (i = 1, n = 0; i < argc; i++) {
if (!strcmp(argv[i], "-a")) {
print_count = 1;
print_solutions = 0;
continue;
}
if (n < SIZE * SIZE)
board[n++] = atoi(argv[i]);
}
while (n < SIZE * SIZE)
board[n++] = 0;
print_board(board, "initial board");
if (validate_board(board, sigs)) {
printf("board is invalid\n");
return 1;
}
/* compute list of empty cells */
for (i = n = 0; i < SIZE * SIZE; i++) {
if (board[i] == 0)
empty[n++] = i;
}
if ((count = try_board(board, sigs, empty, n)) == 0) {
printf("board does not have solutions\n");
return 1;
}
if (print_count) {
printf("total board solutions: %llu\n", count);
}
return 0;
}
传递命令行选项-a
使程序查找所有解决方案并打印找到的总数。正如预期的那样,时间随着空单元格的数量呈指数增长。但它们只有在板子不到一半且有很多解决方案时才变得重要。
我做了一个算法来检查棋盘是否非法,它检查行、列和扇区中的重复项,它对重复项很有效,但在玩了几局之后我得到了以下棋盘:
没有解决方案(没有B5和A7的候选)但没有重复。我们是否总是需要检查是否没有候选人以及检查重复项?还有什么要检查的吗?
只需检查重复项即可让您证明明显的非法填充案例。
恐怕你必须尝试解决数独板才能证明部分填充是合法的,即有解决方案。找到一个解决方案就足够了,但可能还有其他解决方案。找到所有解决方案并不难。
对于您提供的情况,证明没有解决方案很容易,一步就会导致失败(单元格 A7 的所有可能解决方案都会产生重复),但在一般情况下可能会更复杂,因为它需要多个步骤。
一种蛮力方法,例如尝试每个空方块的所有可能性并递归,具有可怕的复杂性。您可以在互联网上找到捷径并用您自己的代码进行研究。
编辑: 如有疑问,请尝试暴力破解!数独在一般情况下可能是 NP-Complete,但在实践中它似乎并没有显着发散。我反思了上面的初步分析,并决定尝试一下蛮力。我写了一个小程序来解决命令行上给出的数独问题。即使在我的旧 MacBook Air 上,它也能在不到几毫秒的时间内解决我尝试过的所有问题。
再次编辑:我用通用版本更新了代码,该版本使用了更好的算法并且快了近 80 倍。
代码如下:
/* Generic Sudoku solver by Charles Gordon */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef GEN
#define GEN 3
#endif
#define SIZE (GEN*GEN)
typedef unsigned char u8_t;
typedef unsigned long long llu_t;
static int print_count = 0, print_solutions = 1;
/* utility arrays to dispatch cell number to signature arrays */
static int rowsig[SIZE * SIZE], colsig[SIZE * SIZE], regsig[SIZE * SIZE];
static void sigcell_init() {
for (int i = 0, row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++, i++) {
rowsig[i] = row;
colsig[i] = SIZE + col;
regsig[i] = SIZE + SIZE + (row / GEN) * GEN + (col / GEN);
}
}
}
static void print_board(const int *board, const char *header) {
printf("%s:\n", header);
for (int i = 0, row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++, i++)
printf("%*.0d", 2 + (SIZE > 9) + (SIZE > 99), board[i]);
putchar('\n');
}
}
static int validate_board(const int *board, u8_t sigs[3 * SIZE][SIZE]) {
memset(sigs, 0, 3 * SIZE * SIZE * sizeof(sigs[0][0]));
for (int i = 0; i < SIZE * SIZE; i++) {
int val = board[i];
if (val == 0)
continue;
if (val < 0 || val > SIZE)
return 2; /* found invalid value */
val -= 1;
if (sigs[rowsig[i]][val] | sigs[colsig[i]][val] | sigs[regsig[i]][val])
return 1; /* found duplicate */
sigs[rowsig[i]][val] = sigs[colsig[i]][val] = sigs[regsig[i]][val] = 1;
}
return 0; /* board is valid */
}
static llu_t try_board(int *board, u8_t sigs[3 * SIZE][SIZE], int *empty, int emp) {
llu_t count, total = 0;
int n, cell;
u8_t *rowsigp, *colsigp, *regsigp;
if (emp == 0) {
if (print_solutions)
print_board(board, "found a board solution");
return 1;
}
cell = *empty; /* next cell to try and populate */
rowsigp = sigs[rowsig[cell]];
colsigp = sigs[colsig[cell]];
regsigp = sigs[regsig[cell]];
for (n = 0; n < SIZE; n++) {
/* check if value is possible */
if ((rowsigp[n] | colsigp[n] | regsigp[n]) == 0) {
rowsigp[n] = colsigp[n] = regsigp[n] = 1;
board[cell] = n + 1;
if ((count = try_board(board, sigs, empty + 1, emp - 1)) > 0) {
total += count;
if (!print_count)
break;
}
rowsigp[n] = colsigp[n] = regsigp[n] = 0;
board[cell] = 0;
}
}
return total;
}
int main(int argc, char *argv[]) {
int board[SIZE * SIZE]; /* board values: empty=0 */
u8_t sigs[3 * SIZE][SIZE]; /* signatures for row, col and regions */
int empty[SIZE * SIZE]; /* list of empty cells */
int i, n;
llu_t count = 0;
sigcell_init();
/* initialize board */
for (i = 1, n = 0; i < argc; i++) {
if (!strcmp(argv[i], "-a")) {
print_count = 1;
print_solutions = 0;
continue;
}
if (n < SIZE * SIZE)
board[n++] = atoi(argv[i]);
}
while (n < SIZE * SIZE)
board[n++] = 0;
print_board(board, "initial board");
if (validate_board(board, sigs)) {
printf("board is invalid\n");
return 1;
}
/* compute list of empty cells */
for (i = n = 0; i < SIZE * SIZE; i++) {
if (board[i] == 0)
empty[n++] = i;
}
if ((count = try_board(board, sigs, empty, n)) == 0) {
printf("board does not have solutions\n");
return 1;
}
if (print_count) {
printf("total board solutions: %llu\n", count);
}
return 0;
}
传递命令行选项-a
使程序查找所有解决方案并打印找到的总数。正如预期的那样,时间随着空单元格的数量呈指数增长。但它们只有在板子不到一半且有很多解决方案时才变得重要。