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 ...
随机推荐
- Web Api 2 怎么支持 Session
Add protected void Application_PostAuthorizeRequest() { System.Web.HttpContext.Current.SetSessionSta ...
- 【转】Hive学习路线图
原文博客出自于:http://blog.fens.me/hadoop-hive-roadmap/ 感谢! Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Ha ...
- Speex for Android
http://blog.csdn.net/chenfeng0104/article/details/7088138在Android开发中,需要录音并发送到对方设备上.这时问题来了,手机常会是GPRS. ...
- HDU 5867 Water problem (模拟)
Water problem 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5867 Description If the numbers ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- css3 动画demo
1)http://www.yyyweb.com/demo/css-cokecan/inner.html 2)页面切换效果demo http://www.yyyweb.com/demo/page-tra ...
- lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...
- MFC消息响应机制 q
MFC消息响应机制分析 1 引言微软公司提供的MFC基本类库(Microsoft Foundation Classes),是进行可视化编程时使用最为流行的一个类 库.MFC封装了大部分Windows ...
- spring+jpg环境下,spring实现文件上传
jsp: <form method="post" action="excel.do?method=inputExcel" enctype="mu ...
- 利用HTML5开发Android(6)---构建HTML5离线应用
需要提供一个cache manifest文件,理出所有需要在离线状态下使用的资源例如 Manifest代码 CACHE MANIFEST #这是注释 images/sound-icon.png ima ...