gpt4 book ai didi

java - 给定二维数组行/列索引,如何制定数独 "sub-grid"索引?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:37:18 25 4
gpt4 key购买 nike

我正致力于用 Java 实现数独网格,而我做不到的最后一件事是将以下逻辑简化为数学公式。

这是对这个问题的跟进:How to get the Sudoku 2D-array index, given its "sub-grid" and "cell-in-the-sub-grid" indexes?

下面是函数,效果很好:

public static final int getGridIndexForCell(int rowIdx_0to8, int colIdx_0to8)  {
int idx = -1;
if(rowIdx_0to8 < 3) {
//Grid-row 1
idx = ((colIdx_0to8 < 3) ? 0
: ((colIdx_0to8 < 6) ? 1 : 2));

} else if(rowIdx_0to8 < 6) {
//Grid-row 2
idx = ((colIdx_0to8 < 3) ? 3
: ((colIdx_0to8 < 6) ? 4 : 5));

} else {
//Grid-row 3
idx = ((colIdx_0to8 < 3) ? 6
: ((colIdx_0to8 < 6) ? 7 : 8));
}
return idx;
}

它根据提供的底层二维数组的行和列索引返回“网格索引”。

这就是我所说的网格(我将整个东西称为“板”):

        |         |
0 | 1 | 2
| |
-------------------------
| |
3 | 4 | 5
| |
-------------------------
| |
6 | 7 | 8
| |

每个网格有九个单元格,索引如下

0 1 2
3 4 5
6 7 8

我会尽可能多地欣赏带注释的答案,因为这是我还没有得到的东西。

最佳答案

所以你想要的公式是

grid_index = (column / 3) + (row / 3) * 3 = column / 3 + row - row % 3

grid_index 每次列增长 3 时增长 1 并且 grid_index 增长 3 每次row 增长 3。同样,所有除法都用整数完成。

关于java - 给定二维数组行/列索引,如何制定数独 "sub-grid"索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22311529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com