1 回答
TA贡献1780条经验 获得超3个赞
你从未达到条件为cell == 81真的状态。我没有进行调试来确切说明原因是什么。
基本上是你的backtrack方法有问题。现在的样子是这样的:
private static boolean backtrack(int[] board, int cell, int value) {
int line = cell / 9;
//check line
for (int i = 0; i < 9; i++) {
if ((board[line * 9 + i] == value)) {
return true;
}
}
int column = cell % 9;
//check column
for (int i = 0; i < 9; i++) {
if (board[column + i * 9] == value) {
return true;
}
}
int squareLine = line - (line % 3);
int squareColumn = column - (column % 3);
//check square
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[(squareLine + i) * 9 + (squareColumn + j)] == value) {
return true;
}
}
}
return false;
}
添加回答
举报