LeetCode37 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. (Hard)
分析:
DFS搜索,但是不太好写。依次尝试所有可能的数字,然后递归地继续向后添加,发现当填入任何数字都无法满足条件(行列九宫格)后返回false(说明前面填错了)。
想清楚递归地过程的话也不是很难理解。
注意事项:
1.每个数字添加后判断本行,本列和所在九宫格即可,不必判断所有数独内容;
2.自己依照上一题isValid函数写的条件判定代码。导致少了一个board[i][j] != '.'的判断(见代码注释部分),导致所有情况都返回false(因为其会对flag['.' - '0']赋值为1,然后判断等于1)这里错了好久才发现。
代码:
class Solution {
private:
bool isValid (vector<vector<char>>& board, int x, int y) {
int flag[] = {};
for (int i = ; i < board.size(); ++i) {
if (board[x][i] != '.') { //少了这句会导致这个判断总是false!!!
if (flag[board[x][i] - ''] == ) {
return false;
}
else {
flag[board[x][i] - ''] = ;
}
}
}
memset(flag,,sizeof(flag));
for (int i = ; i < board.size(); ++i) {
if (board[i][y] != '.') {
if (flag[board[i][y] - ''] == ) {
return false;
}
else {
flag[board[i][y] - ''] = ;
}
}
}
memset(flag,,sizeof(flag));
int sqx = (x / ) * ;
int sqy = (y / ) * ;
for (int i = sqx; i < sqx + ; ++i) {
for (int j = sqy; j < sqy + ; ++j) {
if (board[i][j] != '.') {
if (flag[board[i][j] - ''] == ) {
return false;
}
else {
flag[board[i][j] - ''] = ;
}
}
}
}
return true;
}
bool solvehelper(vector<vector<char>>& board) {
for (int i = ; i < board.size(); ++i) {
for (int j = ; j < board.size(); ++j) {
if (board[i][j] == '.') {
for (int k = ; k < ; ++k) {
board[i][j] = '' + k;
if (isValid(board, i, j)) {
if (solvehelper(board)) {
return true;
}
}
}
board[i][j] = '.';
return false;
}
}
}
return true;
}
public:
void solveSudoku(vector<vector<char>>& board) {
bool b = solvehelper(board);
}
};
LeetCode37 Sudoku Solver的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...
随机推荐
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- 第三百三十天 how can I 坚持
今天是姥姥二周年,不是忘了,是根本就没不知道,没放在心上,正月十九,记下了,人这一辈子. 搞不懂,搞不懂那签. 锥地求泉,先难后易,顺其自然,偶遇知己,携手同行,是签文的关键,我逐个解释给你听.锥地求 ...
- 第二百零三天 how can I 坚持
空虚的有点害怕. 日复一日. 今天做了什么?下班看了个搞笑段子. 上班和刘松聊了一下午,东扯西扯的. 下班玩了两局dota. 想租车去出去玩玩,确实没有什么想去的地方了. 莫巧菲. 哎,未来好可怕啊. ...
- jquery 回车切换 tab功能
挺有趣的,Jquery 回车切换tab功能的实现哦 <html> <head><!--jquery库.js--></head> <body> ...
- Mysql捕捉(网站)应用执行的语句
如题,很多时候我们需要知道某个程序或者网站链接到额数据库到底执行了什么语句,对于MSsql来说, 比较简单,有相对应的事件查看器,但是对于mysql来说,并没有这个组件或者相关配套工具,此时我们可以 ...
- WIFI接入Internet配置过程
转载请注明出处:http://club.gizwits.com/thread-256-1-1.html AirLink模式GoKit2代配置路由SSID和密码方法如下:1.GoKit2上电2.长按Ke ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- webconfig文件serviceHostingEnvironment节点出错的解决方法
在三点五和二版本的配置中可以出现这个节点,但是在4.0是没有的,所以如果框架是4.0的时候要除去这个节点,不然就会报以下错误: Configuration Error Description: An ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
- android: Incorrect line ending: found carriage return (\r) without corresponding newline (\n)
当报这种错误的时候:Incorrect line ending: found carriage return (\r) without corresponding newline (\n) 解决方法: ...