【发布时间】:2013-12-24 04:14:50
【问题描述】:
只是想知道为什么下面的代码
public static void generateSudoku(int[][] num) {
boolean[][][] possibilities = new boolean[9][9][9];
boolean[] noPossibilities = {false, false, false, false, false, false, false, false, false};
boolean[] defaultPossibilities = {true, true, true, true, true, true, true, true, true};
//Get possibilities
for( int j = 0; j < 9; j++) {
for( int i = 0; i < 9; i++) {
if(definite[i][j] == 0) {
possibilities[i][j] = defaultPossibilities;
for(int x = 0; x < 9; x++) {
if(definite[x][j] != 0) {
possibilities[i][j][definite[x][j]-1] = false;
}
}
for(int y = 0; y < 9; y++) {
if(definite[i][y] != 0) {
possibilities[i][j][definite[i][y]-1] = false;
}
}
} else {
possibilities[i][j] = noPossibilities;
}
System.out.println(possibilities[i][j][0] +" "+ possibilities[i][j][1] +" "+ possibilities[i][j][2] +" "+
possibilities[i][j][3] +" "+possibilities[i][j][4] +" "+possibilities[i][j][5] +" "+
possibilities[i][j][6] +" "+possibilities[i][j][7] +" "+possibilities[i][j][8]);
}
}
}
回来了
false false false false false false false false false
false false true true true true true true true
false false false true true true true true true
false false false false true true true true true
false false false false false true true true true
false false false false false false true true true
false false false false false false false true true
false false false false false false false false true
false false false false false false false false false
false false false false false false false false false
//etc... (all false until end of loop).
这意味着返回与相同 x 或相同 y 坐标中其他数字的值相对应的值(即检查在数独游戏中哪些数字是不可能的)。布尔值的位置表示哪些数字是可能的 - 所以如果possibilities[i][j][0] == false,在点 [i][j] 中不可能有 1 值,因为在 x = i 或 y 的任何其他框中都有 1 值= j。
只是想知道为什么它对 j > 0 的所有值都返回 false。
提前致谢
【问题讨论】:
-
possibilities[i][j] = defaultPossibilities;如果这样做,您将开始覆盖defaultPossibilities中的值。 -
我相信调试器就是为这种情况而发明的。
-
@zapl 真的吗?但我不会改变
defaultPossibilities的值吗? -
@zapl 谢谢你是对的。为什么 defaultPossibilities 的值会发生变化?
-
什么是
definite?num是干什么用的?
标签: java algorithm loops logic sudoku