数独箱指数起始位置 [英] Sudoku box indices start position
问题描述
我使用的是回溯实现一个数独解算器。它的形式读取数独板:
I'm implementing a sudoku solver using backtracking. It reads a sudoku board in the form:
<$c$c>027800061000030008910005420500016030000970200070000096700000080006027000030480007$c$c>
我知道我该怎么做计算在列的元素索引%9
(然后做比9简单的等差数列),以及在一个元素通过使用行指数/ 9
(然后通过添加一个,直到我让他们每个人),其中index是一个数字的范围为[0,80]。
I know how I can figure the elements on a column by doing index % 9
(and then doing a simple arithmetic progression of ratio 9) as well the elements on a line by using index/9
(and then by adding one until I get every one of them), where index is a number in the range [0,80].
我无法弄清楚什么是如何得到一箱的起始索引,如果我有那个盒子元素的索引。
What I cannot figure out is how to get the starting index of a box if I have an element's index in that box.
所以,我用Google搜索,我得到:的 http://jakevdp.github.io/blog/2013/04/15/$c$c-golf-in-python-sudoku/
So I googled and I got: http://jakevdp.github.io/blog/2013/04/15/code-golf-in-python-sudoku/
这家伙越来越起始索引在一个盒子里是这样的:
This guy is getting the starting index in a box like this:
开始= 27 * INT(I / 27)+ 3 * INT((I%9)/ 3)
其中,我
是该指数在我的元素列表。
start = 27 * int(i / 27) + 3 * int((i % 9) / 3)
Where i
is the index in my list of elements.
我不明白他是怎么想通了,公式,我怎么能演绎出它自己,所以请解释给我。
I cannot understand how he figured out that formula and how can I deduce it myself, so please explain that to me.
据我所知,名单COM prehension自带这个公式后,这一切才有意义只是没有这个公式。
I understand the list comprehension that comes after this formula, it all makes sense just not this formula.
PS: 我写这篇文章的学习哈斯克尔,但它其实并不重要,因为现在我想要得到这个公式的要点。
PS: I write this to learn Haskell, but it really doesn't matter, since now I want to get the gist of that formula.
推荐答案
首页
表示索引列表中。 blockRow
, blockCol
和 blockIndex
指行/列/该块开始索引。所有的部门是整数除法(四舍五入到下一个整数)。
index
means index into your list. blockRow
, blockCol
and blockIndex
refer to row/column/index of the block start. All divisions are integer divisions (rounding down to next integer).
index = row*9 + col
row = index / 9
col = index % 9
blockRow = (row / 3) * 3
blockCol = (col / 3) * 3
blockRow = (index / 9 / 3) * 3 = (index / 27) * 3
blockCol = (index % 9 / 3) * 3
blockIndex = (blockRow*9) + blockCol = ((index / 27) * 3 * 9) + (index % 9 / 3) * 3 =
(index / 27) * 27 + 3 * (index % 9 / 3)
这篇关于数独箱指数起始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!