leetcode 36. Valid Sudoku 37. Sudoku Solver
2017-11-22 09:30
405 查看
36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
判断一个数独盘的已经填上的部分是不是有效的数独盘。不需要判断没填上的部分。也就是看有没有重复的。
37. 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.
填数独
用递归的思想,先判断每一个空位可以填哪些,然后填进去再递归。
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
判断一个数独盘的已经填上的部分是不是有效的数独盘。不需要判断没填上的部分。也就是看有没有重复的。
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { map<char,int> m2; //判断每一横排 for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board[i][j] == '.') continue; if (m2.find(board[i][j]) == m2.end()) m2[board[i][j]]++; else return false; } m2.clear(); } //判断每一竖排 for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if(board[j][i]=='.') continue; if(m2.find(board[j][i])==m2.end()) m2[board[j][i]]++; else return false; } m2.clear(); } //判断每一个九宫格 for(int k = 0; k < 3; k++) //k控制 i的范围 { for(int h = 0; h < 3; h++) //h控制 j的范围 { for(int i = k * 3; i < k * 3 + 3; i++) { for(int j = h * 3; j < h * 3 + 3; j++) { if(board[i][j]=='.') continue; if(m2.find(board[i][j]) == m2.end()) m2[board[i][j]]++; else return false; } } m2.clear(); } } return true; } };
37. 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.
填数独
用递归的思想,先判断每一个空位可以填哪些,然后填进去再递归。
class Solution { public: int shudu(vector<vector<char>>&board,vector<vector<int>> &mm,int pos,int flag) { if (pos == mm.size()) return 1; int a[10] = {0}; int x = mm[pos][0]; //需要填的位置 int y = mm[pos][1]; for(int i = 0; i < 9; i++) { if (board[x][i] != '.')//横 a[(int(board[x][i])-48)] = 1; if (board[i][y] != '.')//竖 a[(int(board[i][y])-48)] = 1; } //3*3 for (int k = (x/3)*3; k < (x/3)*3+3; k++) { for(int h = (y/3)*3; h<(y/3)*3+3; h++) { if(board[k][h]!='.') a[(int(board[k][h])-48)] = 1; } } //把可以加的加进去 for(int i = 1; i <= 9; i++) { if(a[i] == 0) { board[x][y] = char(i+48); flag = shudu(board,mm,pos+1,flag); if(flag == 0) // 填入之后递归失败 board[x][y] = '.'; else // 填入之后递归成功 break; } } return flag; } void solveSudoku(vector<vector<char>>& board) { //将点('.')的位置存起来 vector<vector<int>> mm; vector<int> m1; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (board[i][j] == '.') { m1.push_back(i); m1.push_back(j); mm.push_back(m1); m1.clear(); } } } //开始遍历 int k = shudu(board, mm, 0, 0); } };
相关文章推荐
- leetcode - 37.Sudoku Solver
- Java [leetcode 37]Sudoku Solver
- LeetCode - 37. Sudoku Solver
- [leetcode 37] Sudoku Solver
- [Leetcode 103] 37 Sudoku Solver
- LeetCode 37: Sudoku Solver
- leetcode[37]Sudoku Solver
- LeetCode 37 SudoKu Solver
- [LeetCode 37] Sudoku Solver回溯解法
- 【LeetCode题意分析&解答】37. Sudoku Solver
- LeetCode 37. Sudoku Solver(数读游戏)
- Leetcode 37 Sudoku Solver 深搜基础题+位运算
- LeetCode_37---Sudoku Solver
- LeetCode题目:37. Sudoku Solver
- LeetCode 37. Sudoku Solver(数独Ⅱ)
- python实现 LeetCode37——Sudoku Solver
- leetcode--37--Sudoku Solver
- LeetCode 37 Sudoku Solver
- [LeetCode]37. Sudoku Solver
- LeetCode 37 Sudoku Solver