Valid Sudoku and Sudoku Solver leetcode
来源:互联网 发布:网络变压器厂招人吗 编辑:程序博客网 时间:2024/05/28 18:45
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.
注意到题目中说的,只要当前已经填充的数字是合法的就可以,不一定要这个数独是有解.(下面说的九宫格都是指3*3的网格)
因此只需要判断9*9网格的每一行、每一列、9个小九宫格是否合法。即如果在每一行、每一列、每个9个小九宫格内,某个数字重复出现了,当前数独就是不合法的。
如果把九宫格按照行从0开始标号,那么数字board[i][j] 位于第 i/3*3+j/3 个九宫格内- 在检测行是否合法时,i 表示矩阵的行,j 表示矩阵的列;
- 检测列是否合法时,i 表示矩阵的列,j 表示矩阵的行;
- 检测九宫格是否合法时,i 表示九宫格的标号,j 表示九宫格里的每个元素(只是我们需要根据i、j定位相应的元素到原来的矩阵:第 i 个九宫格里面的第 j 个元素在原矩阵的第 3*(i/3) + j/3 行,第 3*(i%3) + j%3)列,“/” 表示整数除法)
class Solution {public: bool isValidSudoku(vector<vector<char> > &board) { int rowValid[10] = {0}; int colValid[10] = {0}; int subBoardValid[10] = {0}; for(int i=0;i<9;i++) { memset(rowValid,0,sizeof(rowValid)); memset(colValid,0,sizeof(colValid)); memset(subBoardValid,0,sizeof(subBoardValid)); for(int j=0;j<9;j++) { if(!isValid(rowValid,board[i][j]) || !isValid(colValid,board[j][i]) || !isValid(subBoardValid,board[3*(i/3)+j/3][3*(i%3)+j%3])) { return false; } } } return true; } bool isValid(int arr[],char index) { if(index == '.') return true; if(arr[index-'0'] == 1) return false; arr[index-'0'] = 1; return true; }};
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: void solveSudoku(vector<vector<char> > &board) { solve(board,0); } bool solve(vector<vector<char> > &board,int index) { if(index>80) return true; int i=index/9; int j= index-i*9; if(board[i][j] != '.') return solve(board,index+1); for(int k=1;k<=9;k++) { board[i][j] = '0' + k; if(isValid(board,i,j) && solve(board,index+1)) return true; board[i][j] = '.'; } return false; } bool isValid(vector<vector<char> > &board,int i, int j) { for(int m=0;m<9;m++) { if(m!=i && board[m][j] == board[i][j]) return false; } for(int n=0;n<9;n++) { if(n!=j && board[i][n] == board[i][j]) return false; } for(int m=3*(i/3);m<3*(i/3+1);m++) { for(int n=3*(j/3);n<3*(j/3+1);n++) { if(m!=i && n!=j && board[m][n] == board[i][j]) return false; } } return true; }};
- Valid Sudoku and Sudoku Solver leetcode
- LeetCode: sudoku solver and valid sudoku
- Valid Sudoku and Sudoku Solver
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- Leetcode 36. Valid Sudoku & 37. Sudoku Solver
- LeetCode 36/37. Valid Sudoku/ Sudoku Solver
- LeetCode:Valid Sudoku与Sudoku Solver
- leetcode 36. Valid Sudoku 37. Sudoku Solver
- Valid Sudoku 和 Sudoku Solver
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
- 36. Valid Sudoku && 37. Sudoku Solver
- Leetcode 37 Sudoku Solver
- [Leetcode] Sudoku Solver
- Leetcode 37: Sudoku solver
- LeetCode Sudoku Solver
- chrome浏览器提取网页视频
- C++函数的参数传递机制以及参数的类型选择
- hdu 3853 概率DP 简单
- php面向对象之单例模式--125
- Live555分析(一):VS2008编译
- Valid Sudoku and Sudoku Solver leetcode
- 程序员狂想曲
- Xcode: Delete row shortcut
- Android定时器:实现定时的几种方法
- 传值回调
- 目标跟踪的点跟踪技术(5)
- 关于RSA算法密钥长度/密文长度/明文长度
- php面向对象之-魔术方法__set__get__unset__isset的使用-126
- HDU-#1163 Eddy's digital Roots(九余数定理+同余)