题目:

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的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  3. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  4. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  5. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  6. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  7. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  8. Valid Sudoku&&Sudoku Solver

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  9. 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 ...

随机推荐

  1. Ngix 移动端与Pc端 反向代理判断

    如神马搜索和百度(http://www.baidu.com),当用桌面浏览器和移动浏览器访问的结果是不一样的.其中的手段可能有两种: 转载Ngix反向代理判断 服务端直接判断UA输出不同的界面,JAV ...

  2. China特色创新现状

    1,unity桌面 2,http://www.cs2c.com.cn/ 3,http://os.51cto.com/art/200602/20350.htm 4,http://zhidao.baidu ...

  3. Mellanox OFED2.1-X安装记录

    ---恢复内容开始--- 1,tcl,tk,gcc-gfortran,libnl-devel依赖包

  4. 转】Spark DataFrames入门指南:创建和操作DataFrame

    原博文出自于: http://blog.csdn.net/lw_ghy/article/details/51480358 感谢! 一.从csv文件创建DataFrame 本文将介绍如何从csv文件创建 ...

  5. Spark RDD概念学习系列之RDD的转换(十)

    RDD的转换 Spark会根据用户提交的计算逻辑中的RDD的转换和动作来生成RDD之间的依赖关系,同时这个计算链也就生成了逻辑上的DAG.接下来以“Word Count”为例,详细描述这个DAG生成的 ...

  6. 动软代码生成与 EntityFramework 实体生成模板

    有用到EntityFrameWork的同学们,可以用用. 实体工程中添加EF6的dll 还有 ValidBox4Mvc.ValidRules.dll应用到项目中,此dll下载地址:http://www ...

  7. Flex布局如何让子类在超出边界时隐藏掉

    在flex4中,因为必须添加<s:Scroller>标签才能出现滚动条,如果一个容器例如Panel没有添加滚动条,那么添加到Panel中的child的位置如果超出了Panel的边界,那么这 ...

  8. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. Controlling GameObjects Using Components

    [Accessing Components] The most common case is where a script needs access to other Components attac ...

  10. hdoj 5399 Tpp simple

    WA了一下午.... 1WA:T了,因为阶乘没打表所以时间超了.. 2WA,3WA:runtime error,检查的value数组开小了,应该是MAXN.. 4WA.5WA.6WA:改了改对cnt的 ...