Sudoku Solver
Total Accepted: 20609 Total Submissions: 96910 My Submissions Question Solution
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.
A sudoku puzzle...
...and its solution numbers marked in red.
Hide Tags Backtracking Hash Table
【解题思路】
针对每个没有数的点,对试1到9是否符合要求,如果是,回溯直到得到结果
Total Accepted: 20609 Total Submissions: 96910 My Submissions Question Solution
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.
Hide Tags Backtracking Hash Table
【解题思路】
针对每个没有数的点,对试1到9是否符合要求,如果是,回溯直到得到结果
Java AC
public class Solution {
public int n, size;
public ArrayList<Integer> list;
public void solveSudoku(char[][] board) {
n = board.length;
list = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == '.') {
list.add(i * n + j);
}
}
}
size = list.size();
dfs(board, 0);
}
public boolean dfs(char[][] board, int cur) {
if (cur == size) {
return true;
}
int index = list.get(cur);
int row = index / n;
int col = index % n;
for (int i = 1; i <= 9; i++) {
if (isValidSudoku(board, row, col, (char)(i+'0'))) {
board[row][col] = (char)(i+'0');
if (dfs(board, cur+1)) {
return true;
}
board[row][col] = '.';
}
}
return false;
}
public boolean isValidSudoku(char[][] board, int i, int j, Character value) {
for (int k = 0; k < n; k++) {
if (k == j) {
continue;
}
if (board[i][k] == value) {
// System.out.println("1 "+i+" "+j );
return false;
}
}
for (int k = 0; k < n; k++) {
if (k == i) {
continue;
}
if (board[k][j] == value) {
// System.out.println("2 "+i+" "+j );
return false;
}
}
int rowStart = i / 3 * 3;
int rowEnd = rowStart + 3;
int colStart = j / 3 * 3;
int colEnd = colStart + 3;
for (int k = rowStart; k < rowEnd; k++) {
for (int l = colStart; l < colEnd; l++) {
if (k == i && l == j) {
continue;
}
if (board[k][l] == value) {
// System.out.println("3 "+i+" "+j );
return false;
}
}
}
return true;
}
}
Python AC
class Solution:
# @param board, a 9x9 2D array
# Solve the Sudoku by modifying the input board in-place.
# Do not return any value.
def solveSudoku(self, board):
num = []
n = len(board)
for i in xrange(n):
for j in xrange(n):
if board[i][j] == '.':
num.append(i * n + j)
self.dfs(board, num, len(num), n, 0)
print board
def dfs(self, board, num, size, n, cur):
if cur == size:
return True
index = num[cur]
row = index / n
col = index % n
for i in xrange(1, 10):
if self.isValidSudoku(board, row, col, chr(i+ord('0'))):
temp = board[row]
newTemp = ''
for k in xrange(len(temp)):
if k == col:
newTemp += chr(i+ord('0'))
else:
newTemp += temp[k]
board[row] = newTemp
# board[row][col] = chr(i+ord('0'))
if self.dfs(board, num, size, n, cur+1):
return True
board[row] = temp
return False
def isValidSudoku(self, board, row, col, value):
for i in xrange(9):
if board[i][col] == value:
return False
for j in xrange(9):
if board[row][j] == value:
return False
rowStart = row / 3 * 3
rowEnd = rowStart + 3
colStart = col / 3 * 3
colEnd = colStart + 3
for i in xrange(rowStart, rowEnd):
for j in xrange(colStart, colEnd):
if board[i][j] == '.':
continue
if board[i][j] == value:
return False
return True