[leetcode] Sudoku Solver
2014-09-03 22:59
190 查看
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.
思路:递归,尝试各种可能,参考链接http://blog.csdn.net/doc_sgl/article/details/13005951
代码:
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.
思路:递归,尝试各种可能,参考链接http://blog.csdn.net/doc_sgl/article/details/13005951
代码:
class Solution { public: void solveSudoku(vector<vector<char> > &board) { dfs(board); } bool dfs(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) && dfs(board)) return true; board[i][j]='.'; } return false; } } } return true; } bool isValid(vector<vector<char> > &board,int a,int b){ int i,j; for(i=0;i<9;i++){ if(i!=a && board[i][b]==board[a][b]) return false; } for(j=0;j<9;j++){ if(j!=b && board[a][j]==board[a][b]) return false; } int x=a/3*3; int 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; } };
相关文章推荐
- LeetCode 37 - Sudoku Solver
- LeetCode: Sudoku Solver
- leetcode:Sudoku Solver
- [Leetcode]Sudoku Solver
- leetcode 37:Sudoku Solver
- 132_leetcode_Sudoku Solver
- LeetCode刷题笔录Sudoku Solver
- leetcode题解c++ | 37. Sudoku Solver
- [LeetCode]题解(python):037-Sudoku Solver
- LeetCode_37---Sudoku Solver
- LeetCode 第 37 题 (Sudoku Solver)
- leetcode: Sudoku Solver
- Java [leetcode 37]Sudoku Solver
- leetcode-37-sudoku solver
- [LeetCode] Sudoku Solver 解数独,递归,回溯
- [LeetCode]Sudoku Solver
- LeetCode 37 Sudoku Solver 回溯和深度遍历 还要学习
- [LeetCode] Sudoku Solver 求解数独
- leetCode Sudoku Solver
- Leetcode-Sudoku Solver