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.
Backtracking Hash Table
N皇后问题----需要仔细研究一下,虽然暴力方法仍然可通过
JavaCode:
public class Solution {
public void solveSudoku(char[][] board) {
solve(board);
}
private boolean solve(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j ++) {
if (board[i][j] =='.') {
for (char k = '1'; k <= '9'; k++) {
board[i][j] = k;
if (isValid(board, i, j) && solve(board)) {
return true;
}
board[i][j] = '.';
}
return false;
}
}
}
return true;
}
private boolean isValid(char[][] board,int x, int y ) {
boolean [][] square = new boolean[9][9];
for (int i = 0; i < 9; i++) {
if (y != i &&board[x][y] == board[x][i]){
return false;
}
}
for (int i = 0; i < 9; i++) {
if (x != i &&board[i][y] == board[x][y]){
return false;
}
}
for (int i = (x / 3) * 3; i < (x / 3) * 3 + 3; i++) {
for (int j = (y /3) * 3; j < (y / 3) *3 + 3; j++) {
if (x != i && y != j && board[i][j] == board[x][y]) {
return false;
}
}
}
return true;
}
}
/**
*
*/
package From21;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Hashtable;
/**
* @author MohnSnow
* @time 2015年6月16日 下午3:18:26
*
*/
public class LeetCode37 {
/**
* @param argsmengdx
* -fnst
*/
public static void solveSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
//System.out.println(" i: " + i + ",j: " + j);
solveIt(board);//循环寻找为空的地方去解决,一次解决一个然后再从头开始
i = 0;
j = 0;
}
}
}
}
public static void solveIt(char[][] board) {
Hashtable<ArrayList<Integer>, HashSet<Character>> boardHashtable = new Hashtable<ArrayList<Integer>, HashSet<Character>>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.' && checkIt(board, boardHashtable, i, j)) {
return;//计算填满每一个hashTable,并寻找合适的去填写
}
}
}
}
public static boolean checkIt(char[][] board, Hashtable<ArrayList<Integer>, HashSet<Character>> boardHashtable, int i, int j) {
ArrayList<Integer> aList = new ArrayList<Integer>();
aList.add(i);
aList.add(j);
HashSet<Character> aHashSet = new HashSet<Character>();
for (int m = 0; m < 9; m++) {//row
if (board[i][m] != '.') {
aHashSet.add(board[i][m]);
boardHashtable.put(aList, aHashSet);
}
}
for (int m = 0; m < 9; m++) {//col
if (board[m][j] != '.') {
aHashSet.add(board[m][j]);
boardHashtable.put(aList, aHashSet);
}
}
int aRow = (i / 3) * 3;//计算子队列的对角的行列值
int aCol = (j / 3) * 3;
for (int m = aRow; m < aRow + 3; m++) {//sub matrix
for (int n = aCol; n < aCol + 3; n++) {//sub matrix
if (board[m][n] != '.') {
aHashSet.add(board[m][n]);
boardHashtable.put(aList, aHashSet);
}
}
}
//System.out.println(" i: " + i + ",j: " + j + ". aHashSte" + aHashSet.toString());
if (aHashSet.size() == 8) {
for (int p = 0; p < 10; p++) {
char temp = (char) (p + '0');
if (!aHashSet.contains(temp)) {
board[i][j] = temp;
}
}
System.out.println("成功位置: i: " + i + ",j: " + j);
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
System.out.print(" " + board[x][y]);
}
System.out.println();
}
return true;
} else {
return false;
}
}
public static void main(String[] args) {
char[][] board = {
{ '5', '3', '.', '.', '7', '.', '.', '.', '.' },
{ '6', '.', '.', '1', '9', '5', '.', '.', '.' },
{ '.', '9', '8', '.', '.', '.', '.', '6', '.' },
{ '8', '.', '.', '.', '6', '.', '.', '.', '3' },
{ '4', '.', '.', '8', '.', '3', '.', '.', '1' },
{ '7', '.', '.', '.', '2', '.', '.', '.', '6' },
{ '.', '6', '.', '.', '.', '.', '2', '8', '.' },
{ '.', '.', '.', '4', '1', '9', '.', '.', '5' },
{ '.', '.', '.', '.', '8', '.', '.', '7', '9' }
};
//solveSudoku(board);//解决所有
//solveIt(board);//解决第一个
/*
* for (int i = 0; i < 9; i++) {
* for (int j = 0; j < 9; j++) {
* System.out.print(" " + board[i][j]);
* }
* System.out.println();
* }
*/
//System.out.println("--------------------------------------");
//solveIt(board);//解决第二个
/*
* for (int i = 0; i < 9; i++) {
* for (int j = 0; j < 9; j++) {
* System.out.print(" " + board[i][j]);
* }
* System.out.println();
* }
*/
char[][] board1 = {
{ '.', '.', '9', '7', '4', '8', '.', '.', '.' },
{ '7', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '2', '.', '1', '.', '9', '.', '.', '.' },
{ '.', '.', '7', '.', '.', '.', '2', '4', '.' },
{ '.', '6', '4', '.', '1', '.', '5', '9', '.' },
{ '.', '9', '8', '.', '.', '.', '3', '.', '.' },
{ '.', '.', '.', '8', '.', '3', '.', '2', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '6' },
{ '.', '.', '.', '2', '7', '5', '9', '.', '.' }
};
//System.out.println("判断是否合适:" + isValidSudoku(board1));
//solveIt(board1);//解决第一个
solveSudoku(board1);//解决所有
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(" " + board1[i][j]);
}
System.out.println();
}
/*
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
* solveIt(board1);//解决第一个
*/}
}
class Solution {
public:
bool isValid(vector<vector<char> > &board, int a, int b) {
for (int i = 0; i < 9; ++i) {
if (i != a && board[i][b] == board[a][b])
return false;
}
for (int j = 0; j < 9; ++j) {
if (j != b && board[a][j] == board[a][b])
return false;
}
int x = a / 3 * 3, y = b / 3 * 3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (x + i != a && y + j != b && board[x + i][y + j] == board[a][b])
return false;
}
}
return true;
}
bool solveHelper(vector<vector<char> > &board) {
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] == '.') {
for (int k = 1; k <= 9; ++k) {
board[i][j] = '0' + k;
if (isValid(board, i, j) && solveHelper(board)) {
return true;
}
board[i][j] = '.';
}
return false;
}
}
}
return true;
}
void solveSudoku(vector<vector<char> > &board) {
solveHelper(board);
}
};