https://oj.leetcode.com/problems/sudoku-solver/
http://blog.csdn.net/linhuanmars/article/details/20748761
public class Solution { public void solveSudoku(char[][] board) { resolve(board, 0, 0); } private boolean resolve(char[][] b, // current board int i, // current row int j) // current col { // Goes to next row if (j == 9) return resolve(b, i + 1, 0); // No further to try if (i == 9) return true; // A pre-defined, goes to next if (b[i][j] != ‘.‘) return resolve(b, i , j + 1); // possible numbers for (char k = ‘1‘ ; k <= ‘9‘ ; k ++) { b[i][j] = k; if (checkRow(b, i) && checkCol(b, j) && checkBlk(b, i, j)) { if (resolve(b, i, j + 1)) return true; // else this assignment is not good for following iterations. } b[i][j] = ‘.‘; } return false; } // Check row in b in valid private boolean checkRow(char[][] b, int row) { boolean[] c = new boolean[9]; for (int i = 0 ; i < 9 ; i ++) { if (b[row][i] != ‘.‘) { int v = b[row][i] - ‘1‘; if (c[v]) return false; else c[v] = true; } } return true; } // Check column in b in valid private boolean checkCol(char[][] b, int col) { boolean[] c = new boolean[9]; for (int i = 0 ; i < 9 ; i ++) { if (b[i][col] != ‘.‘) { int v = b[i][col] - ‘1‘; if (c[v]) return false; else c[v] = true; } } return true; } // Check block in b is valid private boolean checkBlk(char[][] b, int row, int col) { int rstart = (row / 3) * 3; int cstart = (col / 3) * 3; boolean[] c = new boolean[9]; for (int i = 0 ; i < 3 ; i ++) { for (int j = 0 ; j < 3 ; j ++) { if (b[i + rstart][j + cstart] != ‘.‘) { int v = b[i + rstart][j + cstart] - ‘1‘; if (c[v]) return false; else c[v] = true; } } } return true; } }
原文地址:http://7371901.blog.51cto.com/7361901/1598592