[LeetCode]Sudoku Solver
来源:互联网 发布:sql 非 编辑:程序博客网 时间:2024/06/05 04:19
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.
DFS搜索,加上,回溯法解题。
<span style="font-size:18px;">class Solution {public: 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=0 ; k<9; ++k){ board[i][j] = '1'+k; if(isValidSudoku(board,i,j)&&solveSudoku(board)){ //走下一步 return true; } board[i][j] = '.';//回溯 } return false; //没有有效解,该解法返回false } } } return true; } bool isValidSudoku(vector<vector<char> > &board,int x,int y) { //检查下一步是否合法 for(int i=0; i<9; ++i){ if(i!=x && board[i][y] == board[x][y]){ return false; } } for(int j=0; j<9; ++j){ if(j!=y && board[x][j] == 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; }};</span>
0 0
- 【LeetCode】Valid Sudoku && Sudoku Solver
- [Leetcode]Sudoku Solver&&Valid Sudoku
- leetcode Valid Sudoku & Sudoku Solver
- Leetcode 37 Sudoku Solver
- [Leetcode] Sudoku Solver
- Leetcode 37: Sudoku solver
- LeetCode Sudoku Solver
- [LeetCode]Sudoku Solver
- Leetcode Sudoku Solver
- LeetCode-Sudoku Solver
- LeetCode - Sudoku Solver
- LeetCode:Sudoku Solver
- 【leetcode】Sudoku Solver
- Leetcode: Sudoku Solver
- leetcode Sudoku Solver
- LeetCode | Sudoku Solver
- LeetCode Sudoku Solver
- [Leetcode] Sudoku Solver (Java)
- 机器学习-逻辑回归-参数迭代公式推导
- dw与sublime的区别 --南大软院大神养成计划
- handle 句柄
- 使用Spring时遇到的bug及解决
- 粒子系统
- [LeetCode]Sudoku Solver
- Spring下载
- MySQL基本语法--函数1
- 如何安全的存储密码
- Bitmap android.graphics.Bitmap.createBitmap(int width, int height, Config config)张第三个参数的含义
- Cocos2D:塔防游戏制作之旅(八)
- 聊聊HTTPS和SSL/TLS协议
- Windows平台下sbt的安装设置
- jdom学习读取XML文件