[LeetCode37]Soduko Solver
来源:互联网 发布:淘宝企业店需要交税吗 编辑:程序博客网 时间:2024/05/17 08:26
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.
The idea is straight forward, put the number in empty place. and check if it is valid and solverable.
Java
public boolean solveSudoku(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]=(char) (k+48);if(validSudoku(board, i, j) && solveSudoku(board) )return true;board[i][j] = '.';}return false;}}}return true; }public boolean validSudoku(char[][] board, int x, int y){for(int i=0;i<9;i++){if(i!=x && board[i][y]==board[x][y])return false;if(i!=y && board[x][i]==board[x][y])return false;}for(int i=3*(x/3);i<3*(x/3+1);i++){for(int j=3*(y/3);j<3*(y/3+1);j++){if(i!=x && j!=y && board[i][j] == board[x][y])return false;}}return true;}c++
bool isValidSudoku2(vector<vector<char>> &board, int x,int y){ int i, j; for(i=0; i<9; i++) if(i!= x && board[i][y] == board[x][y]) return false; for(j = 0; j<9; j++) if(j!= y && board[x][j] == board[x][y]) return false; for(i = 3*(x/3); i<3*(x/3+1); i++){ for(j = 3 *(y/3); j<3*(y/3+1);j++) if(i!=x && j!= y && board[i][j] == board[x][y]) return false; } return true;}bool solveSudoku(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(isValidSudoku2(board,i,j) && solveSudoku(board)) return true; board[i][j] = '.'; } return false; } } } return true;}
0 0
- [LeetCode37]Soduko Solver
- Leetcode37 Sudoku Solver
- LeetCode37. Sudoku Solver
- LeetCode37.Sudoku solver电脑来解数独
- LeetCode37
- Android基础总结 - Soduko
- Leetcode37: Implement strStr()
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Leetcode_sudoku-solver
- Leetcode_Soduku Solver
- Sudoku Solver
- Redis复习笔记5—Redis主从复制
- Timer的缺陷
- SpringMVC常用基础知识
- 海量数据库的查询优化及分页算法方案
- 第一种是文件io流:
- [LeetCode37]Soduko Solver
- 通过 Hibernate 实现
- 第四节Swift 类型的安全检查,类型推断和类型别名
- 〖前端技術 從零單排〗day1.2 HTML5&&CSS3的初步學習心得
- 用yum安装Apache,Mysql,PHP
- Python几点要特别注意!
- 用二叉树实现Trie树
- 没事看看--不再迷茫
- 我的学习方法论