leetCode Sudoku Solver
2014-12-14 22:44
246 查看
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.
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.
class Solution { public: bool avalid(vector<vector<char> > &board,int count, int num) { char chr = '0' + num; int row = count / 9; int col = count % 9; int b_r = row / 3 * 3; int b_c = col / 3 * 3; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) { if(board[b_r + i][b_c + j] == chr)return false; } for(int i = 0; i < 9; i++) if(board[row][i] == chr)return false; for(int i = 0; i < 9; i++) if(board[i][col] == chr)return false; return true; } bool solve(vector<vector<char> > &board, int count) { if(count == 81)return true; int row = count / 9; int col = count % 9; int b_r = row / 3 * 3; int b_c = col / 3 * 3; if(board[row][col] != '.')return solve(board, count + 1); for(int i = 1; i <= 9; i++) { if(avalid(board, count, i)) { board[row][col] = '0' + i; if(solve(board, count))return true; board[row][col] = '.'; } } return false; } void solveSudoku(vector<vector<char> > &board) { solve(board, 0); } };
相关文章推荐
- LeetCode 37 Sudoku Solver(求解数独)(*)
- LeetCode | Sudoku Solver
- [LeetCode][Java] Sudoku Solver
- LeetCode - Sudoku Solver
- LeetCode 37: Sudoku Solver
- [LeetCode]Sudoku Solver
- leetcode 37. Sudoku Solver
- [Leetcode] 37. Sudoku Solver
- Leetcode: 37. Sudoku Solver
- LeetCode(37) Sudoku Solver
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
- LeetCode题目:37. Sudoku Solver
- LeetCode 037 Sudoku Solver
- LeetCode 37. Sudoku Solver
- LeetCode:Valid Sudoku与Sudoku Solver
- leetcode-Sudoku Solver
- [Leetcode]_37 Sudoku Solver
- LeetCode-37. Sudoku Solver (JAVA)(数独游戏解集)
- 【leetcode刷题笔记】Sudoku Solver
- [LeetCode] Sudoku Solver