gpt4 book ai didi

algorithm - 数独、回溯算法

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

数独回溯法

int xx = (pos.getX() / 3) * 3;          
int yy = (pos.getY() / 3) * 3;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
if ((xx + x != pos.getX()) && (yy + y != pos.getY())) {
possible[work[xx + x][yy + y]] = false;

其中 x 和 y =

private byte x;
private byte y;

有人可以解释为什么我们除以三并乘以三吗?

(pos.getY() / 3) * 3;                      
(pos.getX() / 3) * 3;

最佳答案

除法是整数除法,所以它会去除余数。在乘法之后进行整数除法将为您提供正确的 3x3 block 的第一个单元格索引。

例如

pos    0    1    2    3    4    5    6    7    8
/3 0 0 0 1 1 1 2 2 2
*3 0 0 0 3 3 3 6 6 6

关于algorithm - 数独、回溯算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10699627/

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