0

我犹豫应该使用以下两种数据结构中的哪一种来表示数独板,以便通过使用裸单隐藏单技术来解决它。

1.

//using bool array to store candidates of a cell.
int[,] board = new int[9,9];
bool[,] isFixed = new bool[9,9]; // determine cell is fixed.
bool[,,] candidates = new bool[9,9,9];

这样,我们可以通过检查candidates[row, col, n]是真还是假来检查单元格 (row,col) 是否包含候选 n

2.

int[,] board = new int[9,9];
bool[,] isFixed = new bool[9,9]; // determine cell is fixed.
bool[,] row = new bool[9,9]; //row(1,2) = true means number 2 was already appear (is solved or fixed) in 1st row
bool[,] col = new bool[9,9]; //col(1,2) = true means number 2 was already appear (is solved or fixed) in 1st col
bool[,] square3x3 = new bool[9,9]; //square3x3 (1,2) = true means number 2 was already appear (is solved or fixed) in 1st square3x3

这样,我们可以通过检查表达式row[r, n] && col[c, n] && square3x3[r/3 * 3 + c/3, n]是真还是假来检查单元格 (r,c) 是否包含候选 n

当某个单元格用数字 n 求解时,在第一种方式中,我们必须更新某个单元格的 row, col, square3x3 中所有 3x9 单元格的候选者,而在第二种方式中,我们只设置 row[,n] , col [,n] 和 square3x3[,n] 为真。

但我不确定哪种方式适合和有效地找到裸单和隐藏单。

任何人都可以建议我找到隐藏单的算法吗?

帮帮我,谢谢!

4

2 回答 2

0

我个人不会使用必须保持同步的一组基本数组,而是使用 Board 类。

这将在内部有一个 9x9 的 Field 项目数组,其中包含一个可选的 ( int?) 给定数字、一个可选的派生数字和一个bool[9]候选列表 ( )。

此类将公开一些属性/方法以获取特定单元格或行/列/块。

于 2011-12-21T10:20:29.673 回答
-1

当我自己解决数独时,我只使用了两个多维数组。

一个包含字段的当前状态(单元格是数字),另一个 - 可能的下一个字段状态(单元格是数字数组)。

你可能可以从我的代码中得到一些想法(虽然它是用 Ruby 编写的)

https://github.com/stulentsev/sudoku-solver/blob/master/solver.rb

于 2011-12-21T10:16:26.547 回答