1 回答

TA貢獻1780條經(jīng)驗 獲得超4個贊
你從未達到條件為cell == 81真的狀態(tài)。我沒有進行調試來確切說明原因是什么。
基本上是你的backtrack方法有問題?,F(xiàn)在的樣子是這樣的:
? ? 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;
}
添加回答
舉報