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.

A sudoku puzzle...

...and its solution numbers marked in red.

题意:填充数独,返回是否填充成功

思路:dfs

找一个还没填充的位置,尝试填写0-9其中的一个数字,判断是否理。

如果合理,则可以转移到一个同样子问题,所以可以采用递归的方式实现。

bool solveSudoku(vector<vector<char> >&board)

返回是否成功填充当前状态为 board的数独

bool isValid(const vector<vector<char> >&board, int x, int y){
	//检查行
	for(int j = 0; j < 9; ++j) if(j != y && board[x][j] == board[x][y]) return false;
	//检查列
	for(int i = 0; i < 9; ++i) if(i != x && board[i][y] == board[x][y]) return false;
	//检查小方块
	for(int i = 0; i < 3; ++i)
		for(int j = 0; j < 3; ++j){
			if(!(x/3 * 3 + i == x && y /3 * 3 + j == y) && board[x/3 * 3 + i][y /3 * 3 + j] == board[x][y]) return false;
		}
	return true;
}

bool solveSudoku(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 = 0; k < 9; ++k){
					board[i][j] = k + '1' ;
					if(isValid(board, i, j) && solveSudoku(board)) return true;
					board[i][j] = '.';
				}
				return false;
			}
		}
	}
	return true; //漏写了这句,WA了好多次
}

时间: 2024-10-05 04:27:30

Leetcode dfs 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][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 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 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: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] 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.

LeetCode 36 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. 思路1:使用暴力DFS

LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: 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. So