leetcode:sudoku solver

sudoku solver,题意如下:

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.

也就是解普通的数独,开始想到了很naive的模拟方法,从只有一种可能性也就是可以确定的数字开始填充,结果当然是跑不出来的,因为有些地方是不止一种解的,因为这道题在回溯法的分类里,就重新想了一下。

既然有多种解,那就只能一个个可能的试过去,试到一种解就可以输出了。

具体的用可以理解的话来说,过程是这样的:

遍历整个数独表格,一旦碰到一个空位,就往里填个1,然后判断这个1是否在所在的行和列和小的九宫格里是可行的,如果可行,就往下遍历,如果不可行,就填2,重复上面的操作,一直试到9为止。

先贴上代码:

public static void solveSudoku(char[][] board) {
		sudoku(board);
    }

	 static boolean sudoku(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] = (char)('0'+k);
						if(isValid(board, i, j) && sudoku(board)){
							return true;
						}
						board[i][j] = '.';
					}
					return false;
				}
			}
		}
		return true;
	}

	 static boolean isValid(char[][] board,int i,int j){
		for(int a=0;a<9;a++){
			if(board[i][j] == board[i][a] && a!=j)return false;
			if(board[i][j] == board[a][j] && a!=i)return false;
		}
		for(int a=3*(i/3);a<3*(i/3+1);a++){
			for(int b=3*(j/3);b<3*(j/3+1);b++){
				if(a!=i && b!=j && board[a][b] == board[i][j]){
					return false;
				}
			}
		}
		return true;
	}

	public static void main(String[] args) {
		char[][] a = {{'.','.','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','.','.'}};
		solveSudoku(a);
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				System.out.print(a[i][j]+" ");
			}
			System.out.println();
		}
	}

代码中的奥妙,只能意会了。

时间: 2024-10-06 09:42:30

leetcode:sudoku solver的相关文章

【leetcode】 Sudoku Solver

问题: 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. 说明: 数独有

LeetCode 037 Sudoku Solver

题目要求:Sudoku Solver 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

[LeetCode][JavaScript]Sudoku Solver

Sudoku Solver 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 re

Leetcode dfs Sudoku Solver

Sudoku Solver Total Accepted: 11799 Total Submissions: 56732My Submissions 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

Leetcode 38 Sudoku Solver

Sudoku Solver class Solution: # @param {character[][]} board # @return {void} Do not return anything, modify board in-place instead. def solveSudoku(self, board): def check( x, y): temp = board[x][y]; board[x][y] = '.' for i in xrange(9): if board[i]

【leetcode】Sudoku Solver

Sudoku Solver 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 re

LeetCode OJ:Sudoku Solver(数独游戏)

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. dfs,一直寻找,不行

leetcode笔记:Sudoku Solver

一.题目描述 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. The following photo is a sudoku puzzle- -and its solution numbers

[LeetCode] 37. Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column.