Leetcode 37. Sudoku Solver
2016-01-25 06:05
435 查看
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.
这是数独的系列题中的一道,判断是否Valid, 同样使用遍历行遍历列,然后再遍历小方格。这道题还使用到的方法是DFS和回溯,这次题的回溯的方法是将曾经置位的数清掉,即装换为'.'
code is from
爱做饭的小莹子
http://www.cnblogs.com/springfor/p/3884252.html
public class Solution {
public void solveSudoku(char[][] board) {
if (board==null||board.length==0)
return;
helper(board);
}
private boolean helper(char[][] board){
for(int i=0; i<board.length; i++){
for (int j=0; j<board[0].length; j++){
if (board[i][j]=='.'){
for (char num='1'; num<='9'; num++){//尝试
if(isValid(board, i, j, num)){
board[i][j]=num;
if (helper(board))
return true;
else
board[i][j]='.';//回退
}
}
return false;
}
}
}
return true;
}
private boolean isValid(char[][] board, int i, int j, char c){
// check column
for (int row=0; row<9; row++)
if (board[row][j] == c)
return false;
// check row
for (int col=0; col<9; col++)
if (board[i][col]==c)
return false;
// check block
for(int row=i/3*3; row<i/3*3+3; row++)
for (int col=j/3*3; col<j/3*3+3; col++)
if (board[row][col]==c)
return false;
return true;
}
}
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.
这是数独的系列题中的一道,判断是否Valid, 同样使用遍历行遍历列,然后再遍历小方格。这道题还使用到的方法是DFS和回溯,这次题的回溯的方法是将曾经置位的数清掉,即装换为'.'
code is from
爱做饭的小莹子
http://www.cnblogs.com/springfor/p/3884252.html
public class Solution {
public void solveSudoku(char[][] board) {
if (board==null||board.length==0)
return;
helper(board);
}
private boolean helper(char[][] board){
for(int i=0; i<board.length; i++){
for (int j=0; j<board[0].length; j++){
if (board[i][j]=='.'){
for (char num='1'; num<='9'; num++){//尝试
if(isValid(board, i, j, num)){
board[i][j]=num;
if (helper(board))
return true;
else
board[i][j]='.';//回退
}
}
return false;
}
}
}
return true;
}
private boolean isValid(char[][] board, int i, int j, char c){
// check column
for (int row=0; row<9; row++)
if (board[row][j] == c)
return false;
// check row
for (int col=0; col<9; col++)
if (board[i][col]==c)
return false;
// check block
for(int row=i/3*3; row<i/3*3+3; row++)
for (int col=j/3*3; col<j/3*3+3; col++)
if (board[row][col]==c)
return false;
return true;
}
}
相关文章推荐
- 怎样从arraylist中删除指定位置的对象?
- C#的ArrayList集合的使用
- 安装配置Jira和Confluence集成环境
- C#返回arraylist集合长度的方法是什么?C#ArrayList重复数据删除
- Leetcode 36. Valid Sudoku
- Leetcode 242. Valid Anagram
- Mac安装mosquitto错误
- 高度关注!国务院对A股发出强烈信号↓
- 高度关注!国务院对A股发出强烈信号↓
- 人脸识别资源
- java图形
- 人脸识别资源
- java图形
- *Maximum Length Palindromic Sub-Sequence of an Array.
- (You Can Hack It, Architecture and Design) => { Dependency Injection; }
- 堆排序和优先级队列
- 43. Multiply Strings leetcode python 2016 new season
- Count Primes -- LeetCodes (primality test)
- 关于miniconda的安装,配置以及包批量安装和使用
- NLTK内置NaiveBayesClassifier与传统朴素贝叶斯算法差异探究