哪种数据结构代表数独难题,并通过寻找裸单/隐藏单来解决它
我很犹豫应该使用以下两种数据结构中的哪一种来表示数独板,以便使用裸单和隐藏单技术来解决它。
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] && 来检查单元格 (r,c) 是否包含候选 n列[c, n] && square3x3[r/3 * 3 + c/3, n] 当某个单元格用数字 n 求解时为 true 或 false
,第一种方式,我们必须更新 row, col 中所有 3x9 单元格的候选,某个单元格的 square3x3 ,而在第二种方式中,我们只将 row[,n] 、 col[,n] 和 square3x3[,n] 设置为 true 。
但我不确定哪种方式适合且有效地找到裸单身和隐藏单身。
有人可以建议我一个算法来找到隐藏的单身吗?
帮帮我,谢谢!
I'm hesitate about which of following two data structure should be use to represent a sudoku board, in order to solve it by using naked single and hidden single technique.
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];
in this way, we can check if a cell (row,col) contains a candidate n by checking wheather candidates[row, col, n]
is true or false
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
in this way, we can check if a cell (r,c) contains a candidate n by checking wheather expression row[r, n] && col[c, n] && square3x3[r/3 * 3 + c/3, n]
is true or false
when a certain cell is solved with number n, in 1st way, we must update the candidates of all 3x9 cell in row, col, square3x3 of a certain cell, while in 2nd way, we just only set row[,n] , col[,n] and square3x3[,n] to true.
But I'm not sure which way is suitably and efficient to find naked single and hidden single.
Can anybody suggest me a algorithm to find hidden single?
Help me, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人不会使用一组必须保持同步的基本数组,而是使用 Board 类。
这将在内部有一个 9x9 的 Field 项目数组,其中包含一个可选的 (
int?
) 给定数字、一个可选的派生数字和一个列表 (bool[9]
)的候选人。此类将公开一些属性/方法来获取特定的单元格或行/列/块。
I personally wouldn't use a set of basic arrays that you would have to keep in sync, but a Board class.
This would have internally a 9x9 array of Field items, which would hold an optional (
int?
) given number, an optional derived number and a list (bool[9]
) of candidates.This class would expose some properties/methods to get at a specific cell or row/column/block.
当我自己解数独时,我只用了两个多维数组。
一个包含字段的当前状态(单元格是数字),另一个包含字段的可能的下一个状态(单元格是数字数组)。
你也许可以从我的代码中得到一些想法(不过它是用 Ruby 编写的)
https ://github.com/stuentsev/sudoku-solver/blob/master/solver.rb
When I was solving sudoku myself, I got away with just two multi-dimensional arrays.
One was containing current state of the field (cells are numbers) and the other - probable next state of field (cells are arrays of numbers).
You can probably get some ideas from my code (it's in Ruby, though)
https://github.com/stulentsev/sudoku-solver/blob/master/solver.rb