Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

    • The given board contain only digits 1-9 and the character '.'.
    • You may assume that the given Sudoku puzzle will have a single unique solution.
    • The given board size is always 9x9.

判断当前插入的值有没有错误,没有继续填下面的

 class Solution {//剪枝 mytip
public void solveSudoku(char[][] board) {
if(null==board ||0==board.length)
return;
help(board);
}
private boolean help(char[][] board){
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board.length; col++) {
if('.'==board[row][col]){
for (char i = '1'; i <= '9'; i++) {
if(isValidSudoku(board,row,col,i)){
board[row][col]=i;
if(help(board)){
return true;
}
else{
board[row][col]='.';
}
}
}
return false;
}
}
}
return true;
}
private boolean isValidSudoku(char[][] board,int row,int col,char ch) {
for (int i = 0; i <board.length ; i++) {
if(ch==board[row][i]||ch==board[i][col]){
return false;
}
}
int r = row/3*3;
int c = col/3*3;
for (int i = r; i <r+3 ; i++) {
for (int j = c; j < c+3 ; j++) {
if(ch==board[i][j]){
return false;
}
}
} return true;
}
}

另外可以通过以下方式加速

相关题

有效的数独 LeetCode36 https://www.cnblogs.com/zhacai/p/10622779.html

LeetCode-37.Sudok Solver的更多相关文章

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

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

  2. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  3. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  4. [leetcode 37]sudoku solver

    1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...

  5. leetcode 37 Sudoku Solver java

    求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...

  6. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  7. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

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

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

  9. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

  10. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

随机推荐

  1. docker开源仓库Harbor部署笔记

    Harbor介绍Harbor是Vmvare团队开发的开源企业级registry仓库,相比docker官方拥有更丰富的权限权利和完善的架构设计,适用大规模docker集群部署提供仓库服务.项目地址:ht ...

  2. history 命令历史

    在终端中按捉[Ctrl]键的同时[r]键,出现提示:(reverse-i-search),此时你尝试一下输入你以前输入过的命令,当你每输入一个字符 的时候,终端都会滚动显示你的历史命令.当显示到你想找 ...

  3. [JS] Topic - why "strict mode" here

    Ref: Javascript 严格模式详解 使得Javascript在更严格的条件下运行: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全 ...

  4. 【mysql】MySQL以逗号隔开的字符串查询方式整理

    1 单个值查询使用函数进行处理,FIND_IN_SET()实用 SELECT * FROM `by_info` WHERE FIND_IN_SET(',type_id); 2 多个值模糊查询,使用正则 ...

  5. VNC实现Windows远程访问Ubuntu 16.04(无需安装第三方桌面,直接使用自带远程工具)

    本文主要是讲解如果理由VNC实现Windows远程访问Ubuntu 16.04,其实网上有很多类似教程,但是很多需要安装第三方桌面(xfce桌面等等),而且很多人不太喜欢安装第三方桌面,很多人像笔者一 ...

  6. EXCEL 2007施工进度横道图制作步骤及实战练习

    [知识讲解] 1.将图表中的部分图形“隐藏”起来 如果为了实现某种特殊的图表效果,需要将图表中的部分图形“隐藏”起来,除了将该系列删除外(有时候这种方法不能达到所需要的效果),还可以通过下面的方法来实 ...

  7. spring框架排错

    1.格式错误. http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/sp ...

  8. yii2优化 - 开启 Schema 缓存

    开启 Schema 缓存 Schema 缓存是一个特殊的缓存功能,每当你使用活动记录时应该要开启这个缓存功能.如你所知, 活动记录能智能检测数据库对象的集合(例如列名.列类型.约束)而不需要手动地描述 ...

  9. PHP基础语法 【】【】 PHP函数

    <?php //注释语法 /* 多行注释 */ //输出语法 echo "hello"; echo "world","dhakj"; ...

  10. SQL Server2012远程访问设置

    http://jingyan.baidu.com/article/a681b0de3bdb7b3b19434667.html?qq-pf-to=pcqq.group 1.打开SQL server201 ...